Skip to content

Commit

Permalink
shmoverride: do not remove the shmid file if taking the lock fails
Browse files Browse the repository at this point in the history
If shmoverride init fails to take the lock, it likely means it is used
by currently running process - do _not_ remove the file in such a case.
This may happen if LD_PRELOAD leaks to child processes (which seems to
happend on Xwayland when shmoverride is linked with -z initfirst).
But also, if X server is started several times for some reason (for
example user calls it manually), it will abort, but not early enough to
skip shmoverride init.

QubesOS/qubes-issues#8515
  • Loading branch information
marmarek committed Oct 15, 2024
1 parent 1c5794d commit 5873084
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions shmoverride/shmoverride.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,24 +533,26 @@ static int try_init(void)
fputs("snprintf() failed!\n", stderr);
abort();
}
shmid_filename = __shmid_filename;
fprintf(stderr, "shmoverride: running with shm file %s\n", shmid_filename);
fprintf(stderr, "shmoverride: running with shm file %s\n", __shmid_filename);

/* Try to lock the shm.id file (don't rely on whether it exists, a previous
* process might have crashed).
*/
idfd = open(shmid_filename, O_RDWR | O_CLOEXEC | O_CREAT | O_NOCTTY, 0600);
idfd = open(__shmid_filename, O_RDWR | O_CLOEXEC | O_CREAT | O_NOCTTY, 0600);
if (idfd < 0) {
fprintf(stderr, "shmoverride opening %s: %s\n",
shmid_filename, strerror(errno));
__shmid_filename, strerror(errno));
goto cleanup;
}
if (flock(idfd, LOCK_EX | LOCK_NB) < 0) {
fprintf(stderr, "shmoverride flock %s: %s\n",
shmid_filename, strerror(errno));
__shmid_filename, strerror(errno));
/* There is probably an alive process holding the file, give up. */
goto cleanup;
}
/* Save shmid file for cleanup only after taking the lock */
shmid_filename = __shmid_filename;

if (ftruncate(idfd, SHM_ARGS_SIZE) < 0) {
perror("shmoverride ftruncate");
goto cleanup;
Expand Down

0 comments on commit 5873084

Please sign in to comment.