init.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Routines that mimic syscalls, but don't use the user address space or file
  4. * descriptors. Only for init/ and related early init code.
  5. */
  6. #include <linux/init.h>
  7. #include <linux/mount.h>
  8. #include <linux/namei.h>
  9. #include <linux/fs.h>
  10. #include <linux/fs_struct.h>
  11. #include <linux/file.h>
  12. #include <linux/init_syscalls.h>
  13. #include <linux/security.h>
  14. #include "internal.h"
  15. int __init init_pivot_root(const char *new_root, const char *put_old)
  16. {
  17. struct path new_path __free(path_put) = {};
  18. struct path old_path __free(path_put) = {};
  19. int ret;
  20. ret = kern_path(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new_path);
  21. if (ret)
  22. return ret;
  23. ret = kern_path(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_path);
  24. if (ret)
  25. return ret;
  26. return path_pivot_root(&new_path, &old_path);
  27. }
  28. int __init init_mount(const char *dev_name, const char *dir_name,
  29. const char *type_page, unsigned long flags, void *data_page)
  30. {
  31. struct path path;
  32. int ret;
  33. ret = kern_path(dir_name, LOOKUP_FOLLOW, &path);
  34. if (ret)
  35. return ret;
  36. ret = path_mount(dev_name, &path, type_page, flags, data_page);
  37. path_put(&path);
  38. return ret;
  39. }
  40. int __init init_umount(const char *name, int flags)
  41. {
  42. int lookup_flags = LOOKUP_MOUNTPOINT;
  43. struct path path;
  44. int ret;
  45. if (!(flags & UMOUNT_NOFOLLOW))
  46. lookup_flags |= LOOKUP_FOLLOW;
  47. ret = kern_path(name, lookup_flags, &path);
  48. if (ret)
  49. return ret;
  50. return path_umount(&path, flags);
  51. }
  52. int __init init_chdir(const char *filename)
  53. {
  54. struct path path;
  55. int error;
  56. error = kern_path(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  57. if (error)
  58. return error;
  59. error = path_permission(&path, MAY_EXEC | MAY_CHDIR);
  60. if (!error)
  61. set_fs_pwd(current->fs, &path);
  62. path_put(&path);
  63. return error;
  64. }
  65. int __init init_chroot(const char *filename)
  66. {
  67. struct path path;
  68. int error;
  69. error = kern_path(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  70. if (error)
  71. return error;
  72. error = path_permission(&path, MAY_EXEC | MAY_CHDIR);
  73. if (error)
  74. goto dput_and_out;
  75. error = -EPERM;
  76. if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
  77. goto dput_and_out;
  78. error = security_path_chroot(&path);
  79. if (error)
  80. goto dput_and_out;
  81. set_fs_root(current->fs, &path);
  82. dput_and_out:
  83. path_put(&path);
  84. return error;
  85. }
  86. int __init init_chown(const char *filename, uid_t user, gid_t group, int flags)
  87. {
  88. int lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  89. struct path path;
  90. int error;
  91. error = kern_path(filename, lookup_flags, &path);
  92. if (error)
  93. return error;
  94. error = mnt_want_write(path.mnt);
  95. if (!error) {
  96. error = chown_common(&path, user, group);
  97. mnt_drop_write(path.mnt);
  98. }
  99. path_put(&path);
  100. return error;
  101. }
  102. int __init init_chmod(const char *filename, umode_t mode)
  103. {
  104. struct path path;
  105. int error;
  106. error = kern_path(filename, LOOKUP_FOLLOW, &path);
  107. if (error)
  108. return error;
  109. error = chmod_common(&path, mode);
  110. path_put(&path);
  111. return error;
  112. }
  113. int __init init_eaccess(const char *filename)
  114. {
  115. struct path path;
  116. int error;
  117. error = kern_path(filename, LOOKUP_FOLLOW, &path);
  118. if (error)
  119. return error;
  120. error = path_permission(&path, MAY_ACCESS);
  121. path_put(&path);
  122. return error;
  123. }
  124. int __init init_stat(const char *filename, struct kstat *stat, int flags)
  125. {
  126. int lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  127. struct path path;
  128. int error;
  129. error = kern_path(filename, lookup_flags, &path);
  130. if (error)
  131. return error;
  132. error = vfs_getattr(&path, stat, STATX_BASIC_STATS,
  133. flags | AT_NO_AUTOMOUNT);
  134. path_put(&path);
  135. return error;
  136. }
  137. int __init init_mknod(const char *filename, umode_t mode, unsigned int dev)
  138. {
  139. CLASS(filename_kernel, name)(filename);
  140. return filename_mknodat(AT_FDCWD, name, mode, dev);
  141. }
  142. int __init init_link(const char *oldname, const char *newname)
  143. {
  144. CLASS(filename_kernel, old)(oldname);
  145. CLASS(filename_kernel, new)(newname);
  146. return filename_linkat(AT_FDCWD, old, AT_FDCWD, new, 0);
  147. }
  148. int __init init_symlink(const char *oldname, const char *newname)
  149. {
  150. CLASS(filename_kernel, old)(oldname);
  151. CLASS(filename_kernel, new)(newname);
  152. return filename_symlinkat(old, AT_FDCWD, new);
  153. }
  154. int __init init_unlink(const char *pathname)
  155. {
  156. CLASS(filename_kernel, name)(pathname);
  157. return filename_unlinkat(AT_FDCWD, name);
  158. }
  159. int __init init_mkdir(const char *pathname, umode_t mode)
  160. {
  161. CLASS(filename_kernel, name)(pathname);
  162. return filename_mkdirat(AT_FDCWD, name, mode);
  163. }
  164. int __init init_rmdir(const char *pathname)
  165. {
  166. CLASS(filename_kernel, name)(pathname);
  167. return filename_rmdir(AT_FDCWD, name);
  168. }
  169. int __init init_utimes(char *filename, struct timespec64 *ts)
  170. {
  171. struct path path;
  172. int error;
  173. error = kern_path(filename, 0, &path);
  174. if (error)
  175. return error;
  176. error = vfs_utimes(&path, ts);
  177. path_put(&path);
  178. return error;
  179. }
  180. int __init init_dup(struct file *file)
  181. {
  182. int fd;
  183. fd = get_unused_fd_flags(0);
  184. if (fd < 0)
  185. return fd;
  186. fd_install(fd, get_file(file));
  187. return 0;
  188. }