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
1618 return task_exec_state_copy(tsk);1618 return task_exec_state_copy(tsk);
1619}1619}
1620 1620
1621static int copy_fs(u64 clone_flags, struct task_struct *tsk)1621static int copy_fs(u64 clone_flags, struct task_struct *tsk, bool umh)
1622{1622{
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
1624 if (clone_flags & CLONE_FS) {1642 if (clone_flags & CLONE_FS) {
1625 /* tsk->fs is already what we want */1643 /* tsk->fs is already what we want */
1626 read_seqlock_excl(&fs->seq);1644 read_seqlock_excl(&fs->seq);
@@ -1633,7 +1651,7 @@ static int copy_fs(u64 clone_flags, struct task_struct *tsk) · 6 unmodified lines
1633 read_sequnlock_excl(&fs->seq);1651 read_sequnlock_excl(&fs->seq);
1634 return 0;1652 return 0;
1635 }1653 }
1636 tsk->fs = copy_fs_struct(fs);1654 tsk->real_fs = tsk->fs = copy_fs_struct(fs);
1637 if (!tsk->fs)1655 if (!tsk->fs)
1638 return -ENOMEM;1656 return -ENOMEM;
1639 return 0;1657 return 0;
@@ -2277,7 +2295,7 @@ __latent_entropy struct task_struct *copy_process( · 637 unmodified lines
2277 retval = copy_files(clone_flags, p, args->no_files);2295 retval = copy_files(clone_flags, p, args->no_files);
2278 if (retval)2296 if (retval)
2279 goto bad_fork_cleanup_semundo;2297 goto bad_fork_cleanup_semundo;
2280 retval = copy_fs(clone_flags, p);2298 retval = copy_fs(clone_flags, p, args->umh);
2281 if (retval)2299 if (retval)
2282 goto bad_fork_cleanup_files;2300 goto bad_fork_cleanup_files;
2283 retval = copy_sighand(clone_flags, p);2301 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
2819 .exit_signal = (flags & CSIGNAL),2837 .exit_signal = (flags & CSIGNAL),
2820 .fn = fn,2838 .fn = fn,
2821 .fn_arg = arg,2839 .fn_arg = arg,
2840 .umh = 1,
2822 };2841 };
2823 2842
2824 return kernel_clone(&args);2843 return kernel_clone(&args);
@@ -3216,7 +3235,7 @@ static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp · 391 unmodified lines
3216 */3235 */
3217int ksys_unshare(unsigned long unshare_flags)3236int ksys_unshare(unsigned long unshare_flags)
3218{3237{
3219 struct fs_struct *fs, *new_fs = NULL;3238 struct fs_struct *new_fs = NULL;
3220 struct files_struct *new_fd = NULL;3239 struct files_struct *new_fd = NULL;
3221 struct cred *new_cred = NULL;3240 struct cred *new_cred = NULL;
3222 struct nsproxy *new_nsproxy = NULL;3241 struct nsproxy *new_nsproxy = NULL;
@@ -3247,6 +3266,10 @@ int ksys_unshare(unsigned long unshare_flags) · 24 unmodified lines
3247 if (unshare_flags & CLONE_NEWNS)3266 if (unshare_flags & CLONE_NEWNS)
3248 unshare_flags |= CLONE_FS;3267 unshare_flags |= CLONE_FS;
3249 3268
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
3250 err = check_unshare_flags(unshare_flags);3273 err = check_unshare_flags(unshare_flags);
3251 if (err)3274 if (err)
3252 goto bad_unshare_out;3275 goto bad_unshare_out;
@@ -3294,23 +3317,13 @@ int ksys_unshare(unsigned long unshare_flags) · 41 unmodified lines
3294 new_nsproxy = NULL;3317 new_nsproxy = NULL;
3295 }3318 }
3296 3319
3297 task_lock(current);3320 if (new_fs)
3321 new_fs = switch_fs_struct(new_fs);
3298 3322
3299 if (new_fs) {3323 if (new_fd) {
3300 fs = current->fs;3324 guard(task_lock)(current);
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)
3311 swap(current->files, new_fd);3325 swap(current->files, new_fd);
3312 3326 }
3313 task_unlock(current);
3314 3327
3315 if (new_cred) {3328 if (new_cred) {
3316 /* Install the new user namespace */3329 /* 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
186 ret = kcmp_ptr(task1->files, task2->files, KCMP_FILES);186 ret = kcmp_ptr(task1->files, task2->files, KCMP_FILES);
187 break;187 break;
188 case KCMP_FS:188 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);
190 break;190 break;
191 case KCMP_SIGHAND:191 case KCMP_SIGHAND:
192 ret = kcmp_ptr(task1->sighand, task2->sighand, KCMP_SIGHAND);192 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
71 spin_unlock_irq(&current->sighand->siglock);71 spin_unlock_irq(&current->sighand->siglock);
72 72
73 /*73 /*
74 * Initial kernel threads share ther FS with init, in order to74 * Usermodehelper threads get a copy of userspace init's
75 * get the init root directory. But we've now created a new75 * fs_struct. Reset umask to the default.
76 * thread that is going to execve a user process and has its own
77 * 'struct fs_struct'. Reset umask to the default.
78 */76 */
79 current->fs->umask = 0022;77 current->fs->umask = 0022;
80 78

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