devices: Use path to check device type

FreeBSD device numbers cannot be used to check the type of a device, as
they are merely unique filesystem IDs.

As the paths we use have been sanitized with realpath, we can simply use
the path to check if a requested file is an evdev or drm device. This
also allows us to make the check before the file is opened.
This commit is contained in:
Kenny Levinsen 2020-08-01 03:23:56 +02:00
parent dc9c7bff71
commit e129536a08
7 changed files with 102 additions and 46 deletions

View file

@ -1,10 +1,12 @@
#include <string.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/types.h> #include <sys/types.h>
#ifdef __linux__ #if defined(__linux__)
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#endif #endif
#include "compiler.h"
#include "drm.h" #include "drm.h"
// From libdrm // From libdrm
@ -21,6 +23,24 @@ int drm_drop_master(int fd) {
return ioctl(fd, DRM_IOCTL_DROP_MASTER, 0); return ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
} }
static int path_is_drm_card(const char *path) {
static const char prefix[] = "/dev/dri/card";
static const int prefixlen = STRLEN(prefix);
return strncmp(prefix, path, prefixlen) == 0;
}
static int path_is_drm_render(const char *path) {
static const char prefix[] = "/dev/dri/renderD";
static const int prefixlen = STRLEN(prefix);
return strncmp(prefix, path, prefixlen) == 0;
}
int path_is_drm(const char *path) {
return path_is_drm_card(path) || path_is_drm_render(path);
}
#if defined(__linux__)
int dev_is_drm(dev_t device) { int dev_is_drm(dev_t device) {
return major(device) == 226; return major(device) == 226;
} }
#endif

View file

@ -1,9 +1,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/types.h> #include <sys/types.h>
#if defined(__linux__) #if defined(__linux__)
#include <linux/input.h> #include <linux/input.h>
#include <linux/major.h>
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
#include <dev/evdev/input.h> #include <dev/evdev/input.h>
@ -11,12 +13,21 @@
#error Unsupported platform #error Unsupported platform
#endif #endif
#include "compiler.h"
#include "evdev.h" #include "evdev.h"
int path_is_evdev(const char *path) {
static const char prefix[] = "/dev/input/event";
static const size_t prefixlen = STRLEN(prefix);
return strncmp(prefix, path, prefixlen) == 0;
}
int evdev_revoke(int fd) { int evdev_revoke(int fd) {
return ioctl(fd, EVIOCREVOKE, NULL); return ioctl(fd, EVIOCREVOKE, NULL);
} }
#if defined(__linux__)
int dev_is_evdev(dev_t device) { int dev_is_evdev(dev_t device) {
return major(device) == 13; return major(device) == INPUT_MAJOR;
} }
#endif

View file

@ -15,4 +15,6 @@
#define ALWAYS_INLINE inline #define ALWAYS_INLINE inline
#endif #endif
#define STRLEN(s) ((sizeof(s) / sizeof(s[0])) - 1)
#endif #endif

View file

@ -1,10 +1,13 @@
#ifndef _SEATD_DRM_H #ifndef _SEATD_DRM_H
#define _SEATD_DRM_H #define _SEATD_DRM_H
#include <sys/types.h>
int drm_set_master(int fd); int drm_set_master(int fd);
int drm_drop_master(int fd); int drm_drop_master(int fd);
int path_is_drm(const char *path);
#if defined(__linux__)
#include <sys/types.h>
int dev_is_drm(dev_t device); int dev_is_drm(dev_t device);
#endif
#endif #endif

View file

@ -1,9 +1,12 @@
#ifndef _SEATD_EVDEV_H #ifndef _SEATD_EVDEV_H
#define _SEATD_EVDEV_H #define _SEATD_EVDEV_H
#include <sys/types.h>
int evdev_revoke(int fd); int evdev_revoke(int fd);
int path_is_evdev(const char *path);
#if defined(__linux__)
#include <sys/types.h>
int dev_is_evdev(dev_t device); int dev_is_evdev(dev_t device);
#endif
#endif #endif

View file

@ -8,13 +8,19 @@
struct client; struct client;
enum seat_device_type {
SEAT_DEVICE_TYPE_NORMAL,
SEAT_DEVICE_TYPE_EVDEV,
SEAT_DEVICE_TYPE_DRM,
};
struct seat_device { struct seat_device {
int device_id; int device_id;
int fd; int fd;
int ref_cnt; int ref_cnt;
bool active; bool active;
char *path; char *path;
dev_t dev; enum seat_device_type type;
}; };
struct seat { struct seat {

View file

@ -141,6 +141,17 @@ struct seat_device *seat_open_device(struct client *client, const char *path) {
return NULL; return NULL;
} }
enum seat_device_type type;
if (path_is_evdev(sanitized_path)) {
type = SEAT_DEVICE_TYPE_EVDEV;
} else if (path_is_drm(sanitized_path)) {
type = SEAT_DEVICE_TYPE_DRM;
} else {
log_errorf("invalid path '%s'", sanitized_path);
errno = ENOENT;
return NULL;
}
int device_id = 1; int device_id = 1;
for (size_t idx = 0; idx < client->devices.length; idx++) { for (size_t idx = 0; idx < client->devices.length; idx++) {
struct seat_device *device = client->devices.items[idx]; struct seat_device *device = client->devices.items[idx];
@ -164,39 +175,24 @@ struct seat_device *seat_open_device(struct client *client, const char *path) {
return NULL; return NULL;
} }
const char *prefix = "/dev/";
if (strncmp(prefix, sanitized_path, strlen(prefix)) != 0) {
log_errorf("invalid path '%s': expected device in /dev", sanitized_path);
errno = ENOENT;
return NULL;
}
int fd = open(sanitized_path, O_RDWR | O_NOCTTY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK); int fd = open(sanitized_path, O_RDWR | O_NOCTTY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK);
if (fd == -1) { if (fd == -1) {
log_errorf("could not open file: %s", strerror(errno)); log_errorf("could not open file: %s", strerror(errno));
return NULL; return NULL;
} }
struct stat st; switch (type) {
if (fstat(fd, &st) == -1) { case SEAT_DEVICE_TYPE_DRM:
log_errorf("could not fstat: %s", strerror(errno));
close(fd);
errno = EACCES;
return NULL;
}
if (dev_is_drm(st.st_rdev)) {
if (drm_set_master(fd) == -1) { if (drm_set_master(fd) == -1) {
log_debugf("drm_set_master failed: %s", strerror(errno)); log_debugf("drm_set_master failed: %s", strerror(errno));
} }
} else if (dev_is_evdev(st.st_rdev)) { break;
case SEAT_DEVICE_TYPE_EVDEV:
// Nothing to do here // Nothing to do here
} else { break;
// Not a device type we want to share default:
log_errorf("disallowed device type for '%s': %ld", sanitized_path, st.st_rdev); log_error("invalid seat device type");
close(fd); abort();
errno = EACCES;
return NULL;
} }
struct seat_device *device = calloc(1, sizeof(struct seat_device)); struct seat_device *device = calloc(1, sizeof(struct seat_device));
@ -218,8 +214,8 @@ struct seat_device *seat_open_device(struct client *client, const char *path) {
log_debugf("seat: %p, client: %p, path: '%s', device_id: %d", (void *)seat, (void *)client, log_debugf("seat: %p, client: %p, path: '%s', device_id: %d", (void *)seat, (void *)client,
path, device_id); path, device_id);
device->ref_cnt++; device->ref_cnt = 1;
device->dev = st.st_rdev; device->type = type;
device->fd = fd; device->fd = fd;
device->device_id = device_id; device->device_id = device_id;
device->active = true; device->active = true;
@ -252,14 +248,20 @@ int seat_close_device(struct client *client, struct seat_device *seat_device) {
// The ref count hit zero, so destroy the device // The ref count hit zero, so destroy the device
list_del(&client->devices, idx); list_del(&client->devices, idx);
if (seat_device->active && seat_device->fd != -1) { if (seat_device->active && seat_device->fd != -1) {
if (dev_is_drm(seat_device->dev)) { switch (seat_device->type) {
case SEAT_DEVICE_TYPE_DRM:
if (drm_drop_master(seat_device->fd) == -1) { if (drm_drop_master(seat_device->fd) == -1) {
log_debugf("drm_drop_master failed: %s", strerror(errno)); log_debugf("drm_drop_master failed: %s", strerror(errno));
} }
} else if (dev_is_evdev(seat_device->dev)) { break;
case SEAT_DEVICE_TYPE_EVDEV:
if (evdev_revoke(seat_device->fd) == -1) { if (evdev_revoke(seat_device->fd) == -1) {
log_debugf("evdev_revoke failed: %s", strerror(errno)); log_debugf("evdev_revoke failed: %s", strerror(errno));
} }
break;
default:
log_error("invalid seat device type");
abort();
} }
close(seat_device->fd); close(seat_device->fd);
seat_device->fd = -1; seat_device->fd = -1;
@ -277,17 +279,22 @@ static int seat_deactivate_device(struct client *client, struct seat_device *sea
if (!seat_device->active) { if (!seat_device->active) {
return 0; return 0;
} }
if (dev_is_drm(seat_device->dev)) { switch (seat_device->type) {
case SEAT_DEVICE_TYPE_DRM:
if (drm_drop_master(seat_device->fd) == -1) { if (drm_drop_master(seat_device->fd) == -1) {
log_debugf("drm_drop_master failed: %s", strerror(errno));
return -1; return -1;
} }
} else if (dev_is_evdev(seat_device->dev)) { break;
case SEAT_DEVICE_TYPE_EVDEV:
if (evdev_revoke(seat_device->fd) == -1) { if (evdev_revoke(seat_device->fd) == -1) {
log_debugf("evdev_revoke failed: %s", strerror(errno));
return -1; return -1;
} }
} else { break;
errno = EACCES; default:
return -1; log_error("invalid seat device type");
abort();
} }
seat_device->active = false; seat_device->active = false;
return 0; return 0;
@ -301,17 +308,21 @@ static int seat_activate_device(struct client *client, struct seat_device *seat_
if (seat_device->active) { if (seat_device->active) {
return 0; return 0;
} }
if (dev_is_drm(seat_device->dev)) { switch (seat_device->type) {
drm_set_master(seat_device->fd); case SEAT_DEVICE_TYPE_DRM:
if (drm_set_master(seat_device->fd) == -1) {
log_debugf("drmset_master failed: %s", strerror(errno));
}
seat_device->active = true; seat_device->active = true;
} else if (dev_is_evdev(seat_device->dev)) { break;
// We can't do anything here case SEAT_DEVICE_TYPE_EVDEV:
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} else { default:
errno = EACCES; log_error("invalid seat device type");
return -1; abort();
} }
return 0; return 0;
} }