History

Merge tag 'vfs-7.3-rc1.kthread' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs

Pull kthread vfs updates from Christian Brauner: "This stops kernel threads from sharing filesystem state with userspace. This work is about 3 cycles old and has been in -next for about that time. When the kernel boots init_task creates PID 1 and then kthreadd. From that point every kthread and PID 1 share the same fs_struct. That is why pivot_root() has to rewrite the fs_struct of all kthreads. The rewriting exists so that kthreads can use init's filesystem state when they want to. It also means userspace can move the ground out from under the kernel. PID 1 now gets a completely separate fs_struct. All kthreads are anchored in a private SB_KERNMOUNT instance of nullfs that cannot be mounted on and cannot be used to follow other mounts. Userspace init can no longer affect kthread filesystem state and kthreads can no longer affect userspace fs state without explicit opting in to that. Path lookup from a kthread now fails by default. It makes it deliberately hard to offload security sensitive operations into init's filesystem state from a kthread. Places that legitimately need to look something up there opt in through the new scoped_with_init_fs() which temporarily overrides the caller's fs_struct with init's. usermodehelpers remain the only kernel tasks that genuinely share init's filesystem state, since they execute random binaries in the root filesystem (excellent...). The visible result is that /proc/2/root is a nullfs with an empty mountinfo while /proc/1/root is the real root" * tag 'vfs-7.3-rc1.kthread' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: (26 commits) initramfs_test: use test init/exit hooks to override init fs fs: stop rewriting paths for PF_EXITING | PF_DUMPCORE fs: stop rewriting kthread fs structs fs: start all kthreads in nullfs nullfs: make nullfs multi-instance devtmpfs: create private mount namespace fs: add umh argument to struct kernel_clone_args fs: stop sharing fs_struct between init_task and pid 1 af_unix: use scoped_with_init_fs() for coredump socket lookup initramfs: use scoped_with_init_fs() for rootfs unpacking pnfs/blocklayout: use scoped_with_init_fs() for SCSI device lookup ksmbd: use scoped_with_init_fs() for VFS path operations ksmbd: use scoped_with_init_fs() for filesystem info path lookup ksmbd: use scoped_with_init_fs() for share path resolution fs: use scoped_with_init_fs() for kernel_read_file_from_path_initns() coredump: use scoped_with_init_fs() for coredump path resolution btrfs: use scoped_with_init_fs() for update_dev_time() scsi: target: use scoped_with_init_fs() for APTPL metadata scsi: target: use scoped_with_init_fs() for ALUA metadata crypto: ccp: use scoped_with_init_fs() for SEV file access ...

Linus Torvalds committed 3 months ago

Commit fff01502 parents de03b17 b4343ae31 files · +311 −116Browse files

Showing the changes to kernel (3 of 31 files). Show all

Unified · Split

3 files
  1. kernel/fork.c +33 −20
  2. kernel/kcmp.c +1 −1
  3. kernel/umh.c +2 −4
kernel/fork.c+33−20
@@ -1618,9 +1618,27 @@ static int copy_exec_state(u64 clone_flags, struct task_struct *tsk) · 1617 unmodified lines
16181618 return task_exec_state_copy(tsk);
16191619 }
16201620
1621−static int copy_fs(u64 clone_flags, struct task_struct *tsk)
1621+static int copy_fs(u64 clone_flags, struct task_struct *tsk, bool umh)
16221622 {
1623− struct fs_struct *fs = current->fs;
1623+ struct fs_struct *fs;
1624+
1625+ /*
1626+ * Usermodehelper may copy userspace_init_fs filesystem state but
1627+ * they don't get to create mount namespaces, share the
1628+ * filesystem state, or be started from a non-initial mount
1629+ * namespace.
1630+ */
1631+ if (umh) {
1632+ if (clone_flags & (CLONE_NEWNS | CLONE_FS))
1633+ return -EINVAL;
1634+ if (current->nsproxy->mnt_ns != &init_mnt_ns)
1635+ return -EINVAL;
1636+ fs = userspace_init_fs;
1637+ } else {
1638+ fs = current->fs;
1639+ VFS_WARN_ON_ONCE(current->fs != current->real_fs);
1640+ }
1641+
16241642 if (clone_flags & CLONE_FS) {
16251643 /* tsk->fs is already what we want */
16261644 read_seqlock_excl(&fs->seq);
@@ -1633,7 +1651,7 @@ static int copy_fs(u64 clone_flags, struct task_struct *tsk) · 6 unmodified lines
16331651 read_sequnlock_excl(&fs->seq);
16341652 return 0;
16351653 }
1636− tsk->fs = copy_fs_struct(fs);
1654+ tsk->real_fs = tsk->fs = copy_fs_struct(fs);
16371655 if (!tsk->fs)
16381656 return -ENOMEM;
16391657 return 0;
@@ -2277,7 +2295,7 @@ __latent_entropy struct task_struct *copy_process( · 637 unmodified lines
22772295 retval = copy_files(clone_flags, p, args->no_files);
22782296 if (retval)
22792297 goto bad_fork_cleanup_semundo;
2280− retval = copy_fs(clone_flags, p);
2298+ retval = copy_fs(clone_flags, p, args->umh);
22812299 if (retval)
22822300 goto bad_fork_cleanup_files;
22832301 retval = copy_sighand(clone_flags, p);
@@ -2819,6 +2837,7 @@ pid_t user_mode_thread(int (*fn)(void *), void *arg, unsigned long flags) · 535 unmodified lines
28192837 .exit_signal = (flags & CSIGNAL),
28202838 .fn = fn,
28212839 .fn_arg = arg,
2840+ .umh = 1,
28222841 };
28232842
28242843 return kernel_clone(&args);
@@ -3216,7 +3235,7 @@ static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp · 391 unmodified lines
32163235 */
32173236 int ksys_unshare(unsigned long unshare_flags)
32183237 {
3219− struct fs_struct *fs, *new_fs = NULL;
3238+ struct fs_struct *new_fs = NULL;
32203239 struct files_struct *new_fd = NULL;
32213240 struct cred *new_cred = NULL;
32223241 struct nsproxy *new_nsproxy = NULL;
@@ -3247,6 +3266,10 @@ int ksys_unshare(unsigned long unshare_flags) · 24 unmodified lines
32473266 if (unshare_flags & CLONE_NEWNS)
32483267 unshare_flags |= CLONE_FS;
32493268
3269+ /* No unsharing with overriden fs state */
3270+ VFS_WARN_ON_ONCE(unshare_flags & (CLONE_NEWNS | CLONE_FS) &&
3271+ current->fs != current->real_fs);
3272+
32503273 err = check_unshare_flags(unshare_flags);
32513274 if (err)
32523275 goto bad_unshare_out;
@@ -3294,23 +3317,13 @@ int ksys_unshare(unsigned long unshare_flags) · 41 unmodified lines
32943317 new_nsproxy = NULL;
32953318 }
32963319
3297− task_lock(current);
3320+ if (new_fs)
3321+ new_fs = switch_fs_struct(new_fs);
32983322
3299− if (new_fs) {
3300− fs = current->fs;
3301− read_seqlock_excl(&fs->seq);
3302− current->fs = new_fs;
3303− if (--fs->users)
3304− new_fs = NULL;
3305− else
3306− new_fs = fs;
3307− read_sequnlock_excl(&fs->seq);
3308− }
3309−
3310− if (new_fd)
3323+ if (new_fd) {
3324+ guard(task_lock)(current);
33113325 swap(current->files, new_fd);
3312−
3313− task_unlock(current);
3326+ }
33143327
33153328 if (new_cred) {
33163329 /* Install the new user namespace */
kernel/kcmp.c+1−1
@@ -186,7 +186,7 @@ SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type, · 185 unmodified lines
186186 ret = kcmp_ptr(task1->files, task2->files, KCMP_FILES);
187187 break;
188188 case KCMP_FS:
189− ret = kcmp_ptr(task1->fs, task2->fs, KCMP_FS);
189+ ret = kcmp_ptr(task1->real_fs, task2->real_fs, KCMP_FS);
190190 break;
191191 case KCMP_SIGHAND:
192192 ret = kcmp_ptr(task1->sighand, task2->sighand, KCMP_SIGHAND);
kernel/umh.c+2−4
@@ -71,10 +71,8 @@ static int call_usermodehelper_exec_async(void *data) · 70 unmodified lines
7171 spin_unlock_irq(&current->sighand->siglock);
7272
7373 /*
74− * Initial kernel threads share ther FS with init, in order to
75− * get the init root directory. But we've now created a new
76− * thread that is going to execve a user process and has its own
77− * 'struct fs_struct'. Reset umask to the default.
74+ * Usermodehelper threads get a copy of userspace init's
75+ * fs_struct. Reset umask to the default.
7876 */
7977 current->fs->umask = 0022;
8078

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t