fs_struct.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/export.h>
  3. #include <linux/sched/signal.h>
  4. #include <linux/sched/task.h>
  5. #include <linux/fs.h>
  6. #include <linux/path.h>
  7. #include <linux/slab.h>
  8. #include <linux/fs_struct.h>
  9. #include <linux/init_task.h>
  10. #include "internal.h"
  11. /*
  12. * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
  13. * It can block.
  14. */
  15. void set_fs_root(struct fs_struct *fs, const struct path *path)
  16. {
  17. struct path old_root;
  18. path_get(path);
  19. write_seqlock(&fs->seq);
  20. old_root = fs->root;
  21. fs->root = *path;
  22. write_sequnlock(&fs->seq);
  23. if (old_root.dentry)
  24. path_put(&old_root);
  25. }
  26. /*
  27. * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
  28. * It can block.
  29. */
  30. void set_fs_pwd(struct fs_struct *fs, const struct path *path)
  31. {
  32. struct path old_pwd;
  33. path_get(path);
  34. write_seqlock(&fs->seq);
  35. old_pwd = fs->pwd;
  36. fs->pwd = *path;
  37. write_sequnlock(&fs->seq);
  38. if (old_pwd.dentry)
  39. path_put(&old_pwd);
  40. }
  41. static inline int replace_path(struct path *p, const struct path *old, const struct path *new)
  42. {
  43. if (likely(p->dentry != old->dentry || p->mnt != old->mnt))
  44. return 0;
  45. *p = *new;
  46. return 1;
  47. }
  48. void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
  49. {
  50. struct task_struct *g, *p;
  51. struct fs_struct *fs;
  52. int count = 0;
  53. read_lock(&tasklist_lock);
  54. for_each_process_thread(g, p) {
  55. task_lock(p);
  56. fs = p->fs;
  57. if (fs) {
  58. int hits = 0;
  59. write_seqlock(&fs->seq);
  60. hits += replace_path(&fs->root, old_root, new_root);
  61. hits += replace_path(&fs->pwd, old_root, new_root);
  62. while (hits--) {
  63. count++;
  64. path_get(new_root);
  65. }
  66. write_sequnlock(&fs->seq);
  67. }
  68. task_unlock(p);
  69. }
  70. read_unlock(&tasklist_lock);
  71. while (count--)
  72. path_put(old_root);
  73. }
  74. void free_fs_struct(struct fs_struct *fs)
  75. {
  76. path_put(&fs->root);
  77. path_put(&fs->pwd);
  78. kmem_cache_free(fs_cachep, fs);
  79. }
  80. void exit_fs(struct task_struct *tsk)
  81. {
  82. struct fs_struct *fs = tsk->fs;
  83. if (fs) {
  84. int kill;
  85. task_lock(tsk);
  86. read_seqlock_excl(&fs->seq);
  87. tsk->fs = NULL;
  88. kill = !--fs->users;
  89. read_sequnlock_excl(&fs->seq);
  90. task_unlock(tsk);
  91. if (kill)
  92. free_fs_struct(fs);
  93. }
  94. }
  95. struct fs_struct *copy_fs_struct(struct fs_struct *old)
  96. {
  97. struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
  98. /* We don't need to lock fs - think why ;-) */
  99. if (fs) {
  100. fs->users = 1;
  101. fs->in_exec = 0;
  102. seqlock_init(&fs->seq);
  103. fs->umask = old->umask;
  104. read_seqlock_excl(&old->seq);
  105. fs->root = old->root;
  106. path_get(&fs->root);
  107. fs->pwd = old->pwd;
  108. path_get(&fs->pwd);
  109. read_sequnlock_excl(&old->seq);
  110. }
  111. return fs;
  112. }
  113. int unshare_fs_struct(void)
  114. {
  115. struct fs_struct *fs = current->fs;
  116. struct fs_struct *new_fs = copy_fs_struct(fs);
  117. int kill;
  118. if (!new_fs)
  119. return -ENOMEM;
  120. task_lock(current);
  121. read_seqlock_excl(&fs->seq);
  122. kill = !--fs->users;
  123. current->fs = new_fs;
  124. read_sequnlock_excl(&fs->seq);
  125. task_unlock(current);
  126. if (kill)
  127. free_fs_struct(fs);
  128. return 0;
  129. }
  130. EXPORT_SYMBOL_GPL(unshare_fs_struct);
  131. /* to be mentioned only in INIT_TASK */
  132. struct fs_struct init_fs = {
  133. .users = 1,
  134. .seq = __SEQLOCK_UNLOCKED(init_fs.seq),
  135. .umask = 0022,
  136. };