Repository navigation
Description
Summary (EN)
In an X11 session started by ddm (>= 0.3.0), Xorg runs as root inside ddm.service, whose CapabilityBoundingSet omits CAP_IPC_OWNER. The kernel's ipcperms() therefore treats the X server as neither owner nor group and refuses its shmat() on SysV segments that desktop clients create with shmget(..., IPC_CREAT | 0600), so every XShmAttach fails server-side with BadAccess (error 10). On 0.4.x as shipped, PrivateIPC=true additionally hides those segments from Xorg's private IPC namespace outright; since #120 commented that out (0.5.0, master), the missing capability is the last remaining blocker. Clients whose toolkit appears to gate all MIT-SHM modes on a startup SysV probe — e.g. WeChat 4.1.13.x with its bundled static Qt 5.15.14 — end up with SHM disabled and stop repainting. Adding CAP_IPC_OWNER to the bounding set fixes it (verified with a systemd drop-in on UOS 25). services/ddm.service.in on master still has the 15-capability set without it (same in the 0.4.0 and 0.5.0 tags). This is the X11-session twin of treeland#1320 (merged) and ddm#107 (closed unmerged, a reference implementation), which address the same problem for Xwayland running under a DDM session.
环境
- UOS Desktop 25 专业版(OsBuild 21018.102.100),kernel 6.6.0-amd64-desktop
- ddm 0.4.0(UOS 仓库构建,源码与上游一致;master 上
services/ddm.service.in的CapabilityBoundingSet与之相同) - xserver-xorg-core 2:21.1.16-1deepin17,libxcb-shm0 1.17.0-2
- ddm 启动的 X11 会话(
XDG_SESSION_TYPE=x11,kwin_x11) - 与 GPU 无关:
LIBGL_ALWAYS_SOFTWARE=1下同样复现
现象
- 微信 Linux 4.1.13.26(
/usr/bin/wechat→/opt/wechat/wechat,内置静态 Qt 5.15.14):点击会话后主窗口不重绘,只有最大化/还原才能强制刷新;玲珑版同样复现;切到 TreeLand 会话则一切正常。 - 进程检查:微信所有进程没有任何
memfd:xorg映射,也没有 SysV 共享内存段,即它的 Qt 已经把 MIT-SHM 整体关闭了(这是直接观察;「其 Qt 以启动时的 SysV attach 探测作为所有 SHM 模式的总开关」是根据内置 Qt 5.15.14 的字符串与行为推断的,同样的门控逻辑只在系统 Qt 5.15 上用 LD_PRELOAD 验证过)。
诊断
-
在会话内以普通用户运行复现程序(见下文:
shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0600)+xcb_shm_attach_checked)对:0探测,结果:
shm_attach: FAILED error=10 major=130 minor=1(BadAccess,MIT-SHM ShmAttach)。 -
段权限矩阵(用
IPC_SET改写段属主/权限后,以 uid 1000 重新 attach):- 段属主 uid 0 → 成功;模式 0606 / 0666 / 0777 → 成功
- 属主 uid 1000、模式 0600 / 0640 / 0660 → BadAccess
- gid 0 + 0660 → 成功;gid 0 + 0600 → BadAccess
这正是内核
ipcperms()(定义在ipc/util.c,由ipc/shm.c的 shmat 路径调用)的判定表:属主 uid 0 的行走 owner 类;gid 0 的两行走 group 类(Xorg 的 egid 是 0,正好命中段的组);属主 uid 1000 的行 owner/group 都不命中,又没有CAP_IPC_OWNER,就只剩 other 位能放行。 -
Xorg 以 root 运行、是 ddm 的子进程(cgroup
/system.slice/ddm.service),/proc/<pid>/status中CapEff = CapBnd = 00000020656400ef——正好是 ddm.serviceCapabilityBoundingSet的 15 个能力,bit 15(CAP_IPC_OWNER,0x8000)为 0。Xorg 自己的shm_access()检查是通过的(客户端 euid 等于段属主),失败的是内核对shmat()的EACCES。 -
用 drop-in 加上
CapabilityBoundingSet=CAP_IPC_OWNER(同时PrivateIPC=no)重启后:CapEff = 00000020656480ef,复现程序输出shm_attach: OK,微信主进程出现一个memfd:xorg映射(说明其 Qt 恢复了 MIT-SHM;「SysV 探测通过后转用 fd 通道」是据此推断),点击会话恢复正常重绘。 -
关于
PrivateIPC:0.4.0 里仍是PrivateIPC=true(0.5.0 与 master 已在 fix: keep ddm in the init mount namespace #120 注释掉)。本机第一次只去掉 PrivateIPC 时探测仍然失败,说明缺的是能力;对 SysV MIT-SHM 而言两者都得上——命名空间让段不可见,能力集让 root 也 attach 不上。
建议修复
在 services/ddm.service.in 的 CapabilityBoundingSet= 里加上 CAP_IPC_OWNER。理由:
- bounding set 同时会屏蔽文件能力,所以即使走 fix: grant Xwayland cap_ipc_owner for cross-user MIT-SHM #107 那种给 Xorg 二进制
setcap的方案,这一项也必须在集合里。 - 这确实是一次放宽,但范围很窄:
CAP_IPC_OWNER只绕过内核 IPC 属主/权限检查这一类判定;ddm 本来就持有CAP_SYS_ADMIN、CAP_SYS_BOOT等更强的能力。 - Xorg 的
shm_access()仍然按客户端 uid/gid 校验段属主,不会让客户端跨用户 attach 别人的段。 - Xorg 以 root 运行、permitted set 只受 bounding set 限制,所以不需要像 treeland#1320 那样再加
AmbientCapabilities。
复现
把下面这段保存为 shm-attach-probe.c,cc -O1 -o shm-attach-probe shm-attach-probe.c -lxcb -lxcb-shm,在 ddm 启动的 X11 会话里以普通用户运行:
- 受影响的系统:
shm_attach: FAILED error=10 major=130 minor=1 ...(末尾的 shmseg 字段是服务器残留的 errorValue——这条 BadAccess 路径不回填它,值无意义) - 修复后:
shm_attach: OK - 顺手确认:
grep CapEff /proc/$(pgrep -x Xorg)/status,受影响时是00000020656400ef(无 0x8000 位),修复后是00000020656480ef。
shm-attach-probe.c
/* Minimal MIT-SHM probe: does the X server accept an XShmAttach for a SysV
* segment that this unprivileged client created with mode 0600?
*
* Build: cc -O1 -o shm-attach-probe shm-attach-probe.c -lxcb -lxcb-shm
* Run: inside the session whose X server you want to test (uses $DISPLAY).
*
* xcb_request_check() blocks until the server answered, so the (asynchronous)
* X error cannot be missed the way a plain XShmAttach() return value can.
*/
#include <xcb/xcb.h>
#include <xcb/shm.h>
#include <sys/shm.h>
#include <stdio.h>
int main(void)
{
xcb_connection_t *c = xcb_connect(NULL, NULL);
if (xcb_connection_has_error(c)) {
fprintf(stderr, "cannot connect to the X server (check DISPLAY)\n");
return 2;
}
int shmid = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0600);
if (shmid == -1) {
perror("shmget");
return 2;
}
void *addr = shmat(shmid, NULL, 0);
if (addr == (void *)-1) {
perror("shmat");
shmctl(shmid, IPC_RMID, NULL);
return 2;
}
xcb_shm_seg_t seg = xcb_generate_id(c);
xcb_void_cookie_t cookie = xcb_shm_attach_checked(c, seg, shmid, 0);
xcb_generic_error_t *err = xcb_request_check(c, cookie);
int rc = 0;
if (err) {
fprintf(stderr, "shm_attach: FAILED error=%u major=%u minor=%u shmseg=%u\n",
err->error_code, err->major_code, err->minor_code, err->resource_id);
fprintf(stderr, "(10 = BadAccess; major 130 = MIT-SHM; minor 1 = ShmAttach)\n");
rc = 1;
} else {
fprintf(stderr, "shm_attach: OK\n");
xcb_shm_detach(c, seg);
}
shmdt(addr);
shmctl(shmid, IPC_RMID, NULL);
xcb_disconnect(c);
return rc;
}相关
- treeland#1320(已合并):DDM 会话下 Xwayland 的同类问题,
treeland.service改为PrivateIPC=false+AmbientCapabilities=CAP_IPC_OWNER - fix: grant Xwayland cap_ipc_owner for cross-user MIT-SHM #107(未合并的参考实现):给 Xwayland 二进制
setcap cap_ipc_owner=ep - fix: keep ddm in the init mount namespace #120(已合并):注释掉 ddm.service 的
PrivateIPC/ProtectKernelModules,理由是挂载命名空间;没有触及能力集
Activity