init.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_mount(const char *dev_name, const char *dir_name,
  16. const char *type_page, unsigned long flags, void *data_page)
  17. {
  18. struct path path;
  19. int ret;
  20. ret = kern_path(dir_name, LOOKUP_FOLLOW, &path);
  21. if (ret)
  22. return ret;
  23. ret = path_mount(dev_name, &path, type_page, flags, data_page);
  24. path_put(&path);
  25. return ret;
  26. }
  27. int __init init_umount(const char *name, int flags)
  28. {
  29. int lookup_flags = LOOKUP_MOUNTPOINT;
  30. struct path path;
  31. int ret;
  32. if (!(flags & UMOUNT_NOFOLLOW))
  33. lookup_flags |= LOOKUP_FOLLOW;
  34. ret = kern_path(name, lookup_flags, &path);
  35. if (ret)
  36. return ret;
  37. return path_umount(&path, flags);
  38. }
  39. int __init init_chdir(const char *filename)
  40. {
  41. struct path path;
  42. int error;
  43. error = kern_path(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  44. if (error)
  45. return error;
  46. error = path_permission(&path, MAY_EXEC | MAY_CHDIR);
  47. if (!error)
  48. set_fs_pwd(current->fs, &path);
  49. path_put(&path);
  50. return error;
  51. }
  52. int __init init_chroot(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. goto dput_and_out;
  62. error = -EPERM;
  63. if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
  64. goto dput_and_out;
  65. error = security_path_chroot(&path);
  66. if (error)
  67. goto dput_and_out;
  68. set_fs_root(current->fs, &path);
  69. dput_and_out:
  70. path_put(&path);
  71. return error;
  72. }
  73. int __init init_chown(const char *filename, uid_t user, gid_t group, int flags)
  74. {
  75. int lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  76. struct path path;
  77. int error;
  78. error = kern_path(filename, lookup_flags, &path);
  79. if (error)
  80. return error;
  81. error = mnt_want_write(path.mnt);
  82. if (!error) {
  83. error = chown_common(&path, user, group);
  84. mnt_drop_write(path.mnt);
  85. }
  86. path_put(&path);
  87. return error;
  88. }
  89. int __init init_chmod(const char *filename, umode_t mode)
  90. {
  91. struct path path;
  92. int error;
  93. error = kern_path(filename, LOOKUP_FOLLOW, &path);
  94. if (error)
  95. return error;
  96. error = chmod_common(&path, mode);
  97. path_put(&path);
  98. return error;
  99. }
  100. int __init init_eaccess(const char *filename)
  101. {
  102. struct path path;
  103. int error;
  104. error = kern_path(filename, LOOKUP_FOLLOW, &path);
  105. if (error)
  106. return error;
  107. error = path_permission(&path, MAY_ACCESS);
  108. path_put(&path);
  109. return error;
  110. }
  111. int __init init_stat(const char *filename, struct kstat *stat, int flags)
  112. {
  113. int lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  114. struct path path;
  115. int error;
  116. error = kern_path(filename, lookup_flags, &path);
  117. if (error)
  118. return error;
  119. error = vfs_getattr(&path, stat, STATX_BASIC_STATS,
  120. flags | AT_NO_AUTOMOUNT);
  121. path_put(&path);
  122. return error;
  123. }
  124. int __init init_mknod(const char *filename, umode_t mode, unsigned int dev)
  125. {
  126. struct dentry *dentry;
  127. struct path path;
  128. int error;
  129. if (S_ISFIFO(mode) || S_ISSOCK(mode))
  130. dev = 0;
  131. else if (!(S_ISBLK(mode) || S_ISCHR(mode)))
  132. return -EINVAL;
  133. dentry = kern_path_create(AT_FDCWD, filename, &path, 0);
  134. if (IS_ERR(dentry))
  135. return PTR_ERR(dentry);
  136. if (!IS_POSIXACL(path.dentry->d_inode))
  137. mode &= ~current_umask();
  138. error = security_path_mknod(&path, dentry, mode, dev);
  139. if (!error)
  140. error = vfs_mknod(mnt_user_ns(path.mnt), path.dentry->d_inode,
  141. dentry, mode, new_decode_dev(dev));
  142. done_path_create(&path, dentry);
  143. return error;
  144. }
  145. int __init init_link(const char *oldname, const char *newname)
  146. {
  147. struct dentry *new_dentry;
  148. struct path old_path, new_path;
  149. struct user_namespace *mnt_userns;
  150. int error;
  151. error = kern_path(oldname, 0, &old_path);
  152. if (error)
  153. return error;
  154. new_dentry = kern_path_create(AT_FDCWD, newname, &new_path, 0);
  155. error = PTR_ERR(new_dentry);
  156. if (IS_ERR(new_dentry))
  157. goto out;
  158. error = -EXDEV;
  159. if (old_path.mnt != new_path.mnt)
  160. goto out_dput;
  161. mnt_userns = mnt_user_ns(new_path.mnt);
  162. error = may_linkat(mnt_userns, &old_path);
  163. if (unlikely(error))
  164. goto out_dput;
  165. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  166. if (error)
  167. goto out_dput;
  168. error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode,
  169. new_dentry, NULL);
  170. out_dput:
  171. done_path_create(&new_path, new_dentry);
  172. out:
  173. path_put(&old_path);
  174. return error;
  175. }
  176. int __init init_symlink(const char *oldname, const char *newname)
  177. {
  178. struct dentry *dentry;
  179. struct path path;
  180. int error;
  181. dentry = kern_path_create(AT_FDCWD, newname, &path, 0);
  182. if (IS_ERR(dentry))
  183. return PTR_ERR(dentry);
  184. error = security_path_symlink(&path, dentry, oldname);
  185. if (!error)
  186. error = vfs_symlink(mnt_user_ns(path.mnt), path.dentry->d_inode,
  187. dentry, oldname);
  188. done_path_create(&path, dentry);
  189. return error;
  190. }
  191. int __init init_unlink(const char *pathname)
  192. {
  193. return do_unlinkat(AT_FDCWD, getname_kernel(pathname));
  194. }
  195. int __init init_mkdir(const char *pathname, umode_t mode)
  196. {
  197. struct dentry *dentry;
  198. struct path path;
  199. int error;
  200. dentry = kern_path_create(AT_FDCWD, pathname, &path, LOOKUP_DIRECTORY);
  201. if (IS_ERR(dentry))
  202. return PTR_ERR(dentry);
  203. if (!IS_POSIXACL(path.dentry->d_inode))
  204. mode &= ~current_umask();
  205. error = security_path_mkdir(&path, dentry, mode);
  206. if (!error)
  207. error = vfs_mkdir(mnt_user_ns(path.mnt), path.dentry->d_inode,
  208. dentry, mode);
  209. done_path_create(&path, dentry);
  210. return error;
  211. }
  212. int __init init_rmdir(const char *pathname)
  213. {
  214. return do_rmdir(AT_FDCWD, getname_kernel(pathname));
  215. }
  216. int __init init_utimes(char *filename, struct timespec64 *ts)
  217. {
  218. struct path path;
  219. int error;
  220. error = kern_path(filename, 0, &path);
  221. if (error)
  222. return error;
  223. error = vfs_utimes(&path, ts);
  224. path_put(&path);
  225. return error;
  226. }
  227. int __init init_dup(struct file *file)
  228. {
  229. int fd;
  230. fd = get_unused_fd_flags(0);
  231. if (fd < 0)
  232. return fd;
  233. fd_install(fd, get_file(file));
  234. return 0;
  235. }