* ovl: a write refused with EFBIG still clears the set-ID bits
@ 2026-12-03 5:25 Ding Feng
0 siblings, 0 replies; only message in thread
From: Ding Feng @ 2026-12-03 5:25 UTC (permalink / raw)
To: Miklos Szeredi, Amir Goldstein
Cc: Ding Feng, Christian Brauner, Alexander Viro, Jan Kara,
linux-unionfs, linux-fsdevel
Hi,
On overlayfs, a write that the upper filesystem refuses still clears
S_ISUID and S_ISGID. backing_file_write_iter() calls file_remove_privs()
(fs/backing-file.c:305) before the backing file's write runs
generic_write_checks(). ext4 and tmpfs check first, then strip, so a
refused write leaves the mode alone.
A 06777 root-owned file of 2048 bytes; uid 65534 with RLIMIT_FSIZE 1024
writes 10 bytes at offset 4096, which fails with EFBIG. On v7.4-rc5
(x86_64, QEMU):
ext4 mode 6777 -> 6777
tmpfs mode 6777 -> 6777
overlayfs over ext4 mode 6777 -> 0777
It drops privilege, so it is no security problem, but overlayfs differs
from the filesystems below it, and FUSE passthrough shares
backing_file_write_iter(). Stripping after the write would open a window
where a set-ID file has new content, so the fix I have in mind is the
ext4 order: run generic_write_checks() on the overlay file before
file_remove_privs(). Would you take a patch along those lines?
ovl_fallocate() and ovl_copyfile() also strip before the lower call, but
native ext4 fallocate does the same on a refused call, so I left them
out here.
Reproducer, run as root as ./refused_write_setid /mnt/ovl/file:
/*
* As root, create a 06777 file of 2048 bytes; as uid 65534 with RLIMIT_FSIZE 1024, write
* 10 bytes at offset 4096, which fails with EFBIG; print the mode before and after.
*/
#define _GNU_SOURCE
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char **argv)
{
const char *path = argc > 1 ? argv[1] : "file";
struct rlimit rl = { 1024, RLIM_INFINITY };
char buf[2048];
struct stat before, after;
int fd;
fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
memset(buf, 'a', sizeof(buf));
pwrite(fd, buf, sizeof(buf), 0);
fchmod(fd, 06777);
close(fd);
stat(path, &before);
if (fork() == 0) {
signal(SIGXFSZ, SIG_IGN);
setrlimit(RLIMIT_FSIZE, &rl);
if (setgid(65534) || setuid(65534))
_exit(1);
fd = open(path, O_WRONLY);
if (pwrite(fd, "xxxxxxxxxx", 10, 4096) < 0)
perror("pwrite");
_exit(0);
}
wait(NULL);
stat(path, &after);
printf("%s: mode %04o -> %04o\n", path, before.st_mode & 07777, after.st_mode & 07777);
return 0;
}
Thanks,
Ding Feng
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-03 5:25 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 5:25 ovl: a write refused with EFBIG still clears the set-ID bits Ding Feng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox