Skip to content

Commit

Permalink
[drmkms] Properly get dri name for file access. Some additional secur…
Browse files Browse the repository at this point in the history
…ity checks.
  • Loading branch information
antonioginer committed Sep 30, 2023
1 parent 68e733a commit 6a2b03e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
31 changes: 23 additions & 8 deletions custom_video_drmkms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ bool drmkms_timing::test_kernel_user_modes()
// Count the number of existing modes, so it should be +1 when attaching
// a new mode. Could also check the mode name, still better
conn = drmModeGetConnector(fd, m_desktop_output);
if (!conn)
{
log_verbose("DRM/KMS: <%d> (%s) Cannot get connector\n", m_id, __FUNCTION__);
m_kernel_user_modes = false;
return false;
}

first_modes_count = conn->count_modes;
ret = drmModeAttachMode(fd, m_desktop_output, &mode);
drmModeFreeConnector(conn);
Expand Down Expand Up @@ -568,8 +575,8 @@ bool drmkms_timing::init()
int output_position = 0;
for (int num = 0; num < num_devices; num++)
{
// Ignore render nodes
if (devices[num]->available_nodes & 1 << DRM_NODE_PRIMARY)
// Skip non-primary nodes
if (devices[num]->available_nodes & (1 << DRM_NODE_PRIMARY))
drm_name = devices[num]->nodes[DRM_NODE_PRIMARY];
else continue;

Expand All @@ -589,7 +596,10 @@ bool drmkms_timing::init()
log_error("DRM/KMS: <%d> (init) [ERROR] ioctl DRM_CAP_DUMB_BUFFER\n", m_id);

if (!check_dumb)
{
log_error("DRM/KMS: <%d> (init) [ERROR] dumb buffer not supported\n", m_id);
continue;
}

p_res = drmModeGetResources(m_drm_fd);

Expand Down Expand Up @@ -617,6 +627,7 @@ bool drmkms_timing::init()
}
m_desktop_output = p_connector->connector_id;
m_card_id = num;
strcpy(m_drm_name, drm_name);
log_verbose("DRM/KMS: <%d> (init) card %d connector %d id %d name %s selected as primary output\n", m_id, num, i, m_desktop_output, connector_name);

drmModeEncoder *p_encoder = drmModeGetEncoder(m_drm_fd, p_connector->encoder_id);
Expand Down Expand Up @@ -684,9 +695,14 @@ bool drmkms_timing::init()
s_shared_count[m_card_id] = 2;
}
if (!drmIsMaster(m_drm_fd))
{
m_desktop_output = 0;
log_error("DRM/KMS: <%d> (%s) [ERROR] limited DRM rights on this screen\n", m_id, __FUNCTION__);
}
}
}
// If we're here and we have a valid output, we're done.
if (m_desktop_output) break;
}
}

Expand Down Expand Up @@ -756,10 +772,9 @@ int drmkms_timing::get_master_fd()
return -1;
}

snprintf(dev_path, path_length, "/dev/dri/card%d", m_card_id);
if (!access(dev_path, F_OK) == 0)
if (!access(m_drm_name, F_OK) == 0)
{
log_error("DRM/KMS: <%d> (%s) [ERROR] Device %s doesn't exist\n", m_id, __FUNCTION__, dev_path);
log_error("DRM/KMS: <%d> (%s) [ERROR] Device %s doesn't exist\n", m_id, __FUNCTION__, m_drm_name);
return -1;
}

Expand Down Expand Up @@ -805,16 +820,16 @@ int drmkms_timing::get_master_fd()

// CASE 3: m_drm_fd is not a master (and probably not even a valid FD), the currend pid doesn't have master rights
// Or master is owned by a 3rd party app (like a frontend ...)
log_verbose("DRM/KMS: <%d> (%s) Couldn't find a master FD, opening default /dev/dri/card%d\n", m_id, __FUNCTION__, m_card_id);
log_verbose("DRM/KMS: <%d> (%s) Couldn't find a master FD, opening default %s\n", m_id, __FUNCTION__, m_drm_name);

// mark our former hook as invalid
m_hook_fd = -1;

fd = open(dev_path, O_RDWR | O_CLOEXEC);
fd = open(m_drm_name, O_RDWR | O_CLOEXEC);
if (fd < 0)
{
// Oh, we're totally screwed here, worst possible scenario
log_error("DRM/KMS: <%d> (%s) Can't open /dev/dri/card%d, can't get master rights\n", m_id, __FUNCTION__, m_card_id);
log_error("DRM/KMS: <%d> (%s) Can't open %s, can't get master rights\n", m_id, __FUNCTION__, m_drm_name);
return -1;
}

Expand Down
1 change: 1 addition & 0 deletions custom_video_drmkms.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class drmkms_timing : public custom_video
int m_caps = 0;

char m_device_name[32];
char m_drm_name[32];
unsigned int m_desktop_output = 0;
int m_video_modes_position = 0;

Expand Down

0 comments on commit 6a2b03e

Please sign in to comment.