init.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 = inode_permission(path.dentry->d_inode, 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 = inode_permission(path.dentry->d_inode, 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 = inode_permission(d_inode(path.dentry), 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(path.dentry->d_inode, dentry, mode,
  141. 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. int error;
  150. error = kern_path(oldname, 0, &old_path);
  151. if (error)
  152. return error;
  153. new_dentry = kern_path_create(AT_FDCWD, newname, &new_path, 0);
  154. error = PTR_ERR(new_dentry);
  155. if (IS_ERR(new_dentry))
  156. goto out;
  157. error = -EXDEV;
  158. if (old_path.mnt != new_path.mnt)
  159. goto out_dput;
  160. error = may_linkat(&old_path);
  161. if (unlikely(error))
  162. goto out_dput;
  163. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  164. if (error)
  165. goto out_dput;
  166. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry,
  167. NULL);
  168. out_dput:
  169. done_path_create(&new_path, new_dentry);
  170. out:
  171. path_put(&old_path);
  172. return error;
  173. }
  174. int __init init_symlink(const char *oldname, const char *newname)
  175. {
  176. struct dentry *dentry;
  177. struct path path;
  178. int error;
  179. dentry = kern_path_create(AT_FDCWD, newname, &path, 0);
  180. if (IS_ERR(dentry))
  181. return PTR_ERR(dentry);
  182. error = security_path_symlink(&path, dentry, oldname);
  183. if (!error)
  184. error = vfs_symlink(path.dentry->d_inode, dentry, oldname);
  185. done_path_create(&path, dentry);
  186. return error;
  187. }
  188. int __init init_unlink(const char *pathname)
  189. {
  190. return do_unlinkat(AT_FDCWD, getname_kernel(pathname));
  191. }
  192. int __init init_mkdir(const char *pathname, umode_t mode)
  193. {
  194. struct dentry *dentry;
  195. struct path path;
  196. int error;
  197. dentry = kern_path_create(AT_FDCWD, pathname, &path, LOOKUP_DIRECTORY);
  198. if (IS_ERR(dentry))
  199. return PTR_ERR(dentry);
  200. if (!IS_POSIXACL(path.dentry->d_inode))
  201. mode &= ~current_umask();
  202. error = security_path_mkdir(&path, dentry, mode);
  203. if (!error)
  204. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  205. done_path_create(&path, dentry);
  206. return error;
  207. }
  208. int __init init_rmdir(const char *pathname)
  209. {
  210. return do_rmdir(AT_FDCWD, getname_kernel(pathname));
  211. }
  212. int __init init_utimes(char *filename, struct timespec64 *ts)
  213. {
  214. struct path path;
  215. int error;
  216. error = kern_path(filename, 0, &path);
  217. if (error)
  218. return error;
  219. error = vfs_utimes(&path, ts);
  220. path_put(&path);
  221. return error;
  222. }
  223. int __init init_dup(struct file *file)
  224. {
  225. int fd;
  226. fd = get_unused_fd_flags(0);
  227. if (fd < 0)
  228. return fd;
  229. fd_install(fd, get_file(file));
  230. return 0;
  231. }