fs_struct.c 3.4 KB

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