From: Ding Feng <dingf3ng@gmail.com>
To: Miklos Szeredi <miklos@szeredi.hu>, Amir Goldstein <amir73il@gmail.com>
Cc: Ding Feng <dingf3ng@gmail.com>,
Christian Brauner <brauner@kernel.org>,
Alexander Viro <viro@zeniv.linux.org.uk>, Jan Kara <jack@suse.cz>,
linux-unionfs@vger.kernel.org, linux-fsdevel@vger.kernel.org
Subject: ovl: a write refused with EFBIG still clears the set-ID bits
Date: Thu, 3 Dec 2026 13:25:06 +0800 [thread overview]
Message-ID: <20261203052506.53945-1-dingf3ng@gmail.com> (raw)
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
reply other threads:[~2026-12-03 5:25 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203052506.53945-1-dingf3ng@gmail.com \
--to=dingf3ng@gmail.com \
--cc=amir73il@gmail.com \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-unionfs@vger.kernel.org \
--cc=miklos@szeredi.hu \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox