All of lore.kernel.org
 help / color / mirror / Atom feed
* ovl: a write refused with EFBIG still clears the set-ID bits
@ 2026-12-03  5:25 Ding Feng
  2026-12-03 11:38 ` Amir Goldstein
  0 siblings, 1 reply; 2+ messages 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] 2+ messages in thread

* Re: ovl: a write refused with EFBIG still clears the set-ID bits
  2026-12-03  5:25 ovl: a write refused with EFBIG still clears the set-ID bits Ding Feng
@ 2026-12-03 11:38 ` Amir Goldstein
  0 siblings, 0 replies; 2+ messages in thread
From: Amir Goldstein @ 2026-12-03 11:38 UTC (permalink / raw)
  To: Ding Feng
  Cc: Miklos Szeredi, Christian Brauner, Alexander Viro, Jan Kara,
	linux-unionfs, linux-fsdevel

On Thu, Dec 3, 2026 at 6:25 AM Ding Feng <dingf3ng@gmail.com> wrote:
>
> 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?
>

Hi Ding,

Sorry, but Linux does not make any guarantee about whether the
set-ID bits are cleared when a write fails.

EFBIG happens to fail before the strip on ext4, but that is an
implementation detail, not something overlayfs needs to copy.

> 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.

I think this proves the point above.

Thanks,
Amir.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03 11:38 UTC | newest]

Thread overview: 2+ messages (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
2026-12-03 11:38 ` Amir Goldstein

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.