open.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. /*
  2. * linux/fs/open.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/string.h>
  7. #include <linux/mm.h>
  8. #include <linux/file.h>
  9. #include <linux/fdtable.h>
  10. #include <linux/fsnotify.h>
  11. #include <linux/module.h>
  12. #include <linux/tty.h>
  13. #include <linux/namei.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/capability.h>
  16. #include <linux/securebits.h>
  17. #include <linux/security.h>
  18. #include <linux/mount.h>
  19. #include <linux/fcntl.h>
  20. #include <linux/slab.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/fs.h>
  23. #include <linux/personality.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/audit.h>
  28. #include <linux/falloc.h>
  29. #include <linux/fs_struct.h>
  30. #include <linux/ima.h>
  31. #include <linux/dnotify.h>
  32. #include <linux/compat.h>
  33. #include "internal.h"
  34. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  35. struct file *filp)
  36. {
  37. int ret;
  38. struct iattr newattrs;
  39. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  40. if (length < 0)
  41. return -EINVAL;
  42. newattrs.ia_size = length;
  43. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  44. if (filp) {
  45. newattrs.ia_file = filp;
  46. newattrs.ia_valid |= ATTR_FILE;
  47. }
  48. /* Remove suid, sgid, and file capabilities on truncate too */
  49. ret = dentry_needs_remove_privs(dentry);
  50. if (ret < 0)
  51. return ret;
  52. if (ret)
  53. newattrs.ia_valid |= ret | ATTR_FORCE;
  54. inode_lock(dentry->d_inode);
  55. /* Note any delegations or leases have already been broken: */
  56. ret = notify_change(dentry, &newattrs, NULL);
  57. inode_unlock(dentry->d_inode);
  58. return ret;
  59. }
  60. long vfs_truncate(const struct path *path, loff_t length)
  61. {
  62. struct inode *inode;
  63. long error;
  64. inode = path->dentry->d_inode;
  65. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  66. if (S_ISDIR(inode->i_mode))
  67. return -EISDIR;
  68. if (!S_ISREG(inode->i_mode))
  69. return -EINVAL;
  70. error = mnt_want_write(path->mnt);
  71. if (error)
  72. goto out;
  73. error = inode_permission(inode, MAY_WRITE);
  74. if (error)
  75. goto mnt_drop_write_and_out;
  76. error = -EPERM;
  77. if (IS_APPEND(inode))
  78. goto mnt_drop_write_and_out;
  79. error = get_write_access(inode);
  80. if (error)
  81. goto mnt_drop_write_and_out;
  82. /*
  83. * Make sure that there are no leases. get_write_access() protects
  84. * against the truncate racing with a lease-granting setlease().
  85. */
  86. error = break_lease(inode, O_WRONLY);
  87. if (error)
  88. goto put_write_and_out;
  89. error = locks_verify_truncate(inode, NULL, length);
  90. if (!error)
  91. error = security_path_truncate(path);
  92. if (!error)
  93. error = do_truncate(path->dentry, length, 0, NULL);
  94. put_write_and_out:
  95. put_write_access(inode);
  96. mnt_drop_write_and_out:
  97. mnt_drop_write(path->mnt);
  98. out:
  99. return error;
  100. }
  101. EXPORT_SYMBOL_GPL(vfs_truncate);
  102. long do_sys_truncate(const char __user *pathname, loff_t length)
  103. {
  104. unsigned int lookup_flags = LOOKUP_FOLLOW;
  105. struct path path;
  106. int error;
  107. if (length < 0) /* sorry, but loff_t says... */
  108. return -EINVAL;
  109. retry:
  110. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  111. if (!error) {
  112. error = vfs_truncate(&path, length);
  113. path_put(&path);
  114. }
  115. if (retry_estale(error, lookup_flags)) {
  116. lookup_flags |= LOOKUP_REVAL;
  117. goto retry;
  118. }
  119. return error;
  120. }
  121. SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
  122. {
  123. return do_sys_truncate(path, length);
  124. }
  125. #ifdef CONFIG_COMPAT
  126. COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
  127. {
  128. return do_sys_truncate(path, length);
  129. }
  130. #endif
  131. long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  132. {
  133. struct inode *inode;
  134. struct dentry *dentry;
  135. struct fd f;
  136. int error;
  137. error = -EINVAL;
  138. if (length < 0)
  139. goto out;
  140. error = -EBADF;
  141. f = fdget(fd);
  142. if (!f.file)
  143. goto out;
  144. /* explicitly opened as large or we are on 64-bit box */
  145. if (f.file->f_flags & O_LARGEFILE)
  146. small = 0;
  147. dentry = f.file->f_path.dentry;
  148. inode = dentry->d_inode;
  149. error = -EINVAL;
  150. if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
  151. goto out_putf;
  152. error = -EINVAL;
  153. /* Cannot ftruncate over 2^31 bytes without large file support */
  154. if (small && length > MAX_NON_LFS)
  155. goto out_putf;
  156. error = -EPERM;
  157. /* Check IS_APPEND on real upper inode */
  158. if (IS_APPEND(file_inode(f.file)))
  159. goto out_putf;
  160. sb_start_write(inode->i_sb);
  161. error = locks_verify_truncate(inode, f.file, length);
  162. if (!error)
  163. error = security_path_truncate(&f.file->f_path);
  164. if (!error)
  165. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
  166. sb_end_write(inode->i_sb);
  167. out_putf:
  168. fdput(f);
  169. out:
  170. return error;
  171. }
  172. SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
  173. {
  174. return do_sys_ftruncate(fd, length, 1);
  175. }
  176. #ifdef CONFIG_COMPAT
  177. COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
  178. {
  179. return do_sys_ftruncate(fd, length, 1);
  180. }
  181. #endif
  182. /* LFS versions of truncate are only needed on 32 bit machines */
  183. #if BITS_PER_LONG == 32
  184. SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
  185. {
  186. return do_sys_truncate(path, length);
  187. }
  188. SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
  189. {
  190. return do_sys_ftruncate(fd, length, 0);
  191. }
  192. #endif /* BITS_PER_LONG == 32 */
  193. int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  194. {
  195. struct inode *inode = file_inode(file);
  196. long ret;
  197. if (offset < 0 || len <= 0)
  198. return -EINVAL;
  199. /* Return error if mode is not supported */
  200. if (mode & ~FALLOC_FL_SUPPORTED_MASK)
  201. return -EOPNOTSUPP;
  202. /* Punch hole and zero range are mutually exclusive */
  203. if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
  204. (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
  205. return -EOPNOTSUPP;
  206. /* Punch hole must have keep size set */
  207. if ((mode & FALLOC_FL_PUNCH_HOLE) &&
  208. !(mode & FALLOC_FL_KEEP_SIZE))
  209. return -EOPNOTSUPP;
  210. /* Collapse range should only be used exclusively. */
  211. if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
  212. (mode & ~FALLOC_FL_COLLAPSE_RANGE))
  213. return -EINVAL;
  214. /* Insert range should only be used exclusively. */
  215. if ((mode & FALLOC_FL_INSERT_RANGE) &&
  216. (mode & ~FALLOC_FL_INSERT_RANGE))
  217. return -EINVAL;
  218. /* Unshare range should only be used with allocate mode. */
  219. if ((mode & FALLOC_FL_UNSHARE_RANGE) &&
  220. (mode & ~(FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_KEEP_SIZE)))
  221. return -EINVAL;
  222. if (!(file->f_mode & FMODE_WRITE))
  223. return -EBADF;
  224. /*
  225. * We can only allow pure fallocate on append only files
  226. */
  227. if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
  228. return -EPERM;
  229. if (IS_IMMUTABLE(inode))
  230. return -EPERM;
  231. /*
  232. * We cannot allow any fallocate operation on an active swapfile
  233. */
  234. if (IS_SWAPFILE(inode))
  235. return -ETXTBSY;
  236. /*
  237. * Revalidate the write permissions, in case security policy has
  238. * changed since the files were opened.
  239. */
  240. ret = security_file_permission(file, MAY_WRITE);
  241. if (ret)
  242. return ret;
  243. if (S_ISFIFO(inode->i_mode))
  244. return -ESPIPE;
  245. if (S_ISDIR(inode->i_mode))
  246. return -EISDIR;
  247. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  248. return -ENODEV;
  249. /* Check for wrap through zero too */
  250. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  251. return -EFBIG;
  252. if (!file->f_op->fallocate)
  253. return -EOPNOTSUPP;
  254. file_start_write(file);
  255. ret = file->f_op->fallocate(file, mode, offset, len);
  256. /*
  257. * Create inotify and fanotify events.
  258. *
  259. * To keep the logic simple always create events if fallocate succeeds.
  260. * This implies that events are even created if the file size remains
  261. * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
  262. */
  263. if (ret == 0)
  264. fsnotify_modify(file);
  265. file_end_write(file);
  266. return ret;
  267. }
  268. EXPORT_SYMBOL_GPL(vfs_fallocate);
  269. int ksys_fallocate(int fd, int mode, loff_t offset, loff_t len)
  270. {
  271. struct fd f = fdget(fd);
  272. int error = -EBADF;
  273. if (f.file) {
  274. error = vfs_fallocate(f.file, mode, offset, len);
  275. fdput(f);
  276. }
  277. return error;
  278. }
  279. SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
  280. {
  281. return ksys_fallocate(fd, mode, offset, len);
  282. }
  283. /*
  284. * access() needs to use the real uid/gid, not the effective uid/gid.
  285. * We do this by temporarily clearing all FS-related capabilities and
  286. * switching the fsuid/fsgid around to the real ones.
  287. */
  288. long do_faccessat(int dfd, const char __user *filename, int mode)
  289. {
  290. const struct cred *old_cred;
  291. struct cred *override_cred;
  292. struct path path;
  293. struct inode *inode;
  294. int res;
  295. unsigned int lookup_flags = LOOKUP_FOLLOW;
  296. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  297. return -EINVAL;
  298. override_cred = prepare_creds();
  299. if (!override_cred)
  300. return -ENOMEM;
  301. override_cred->fsuid = override_cred->uid;
  302. override_cred->fsgid = override_cred->gid;
  303. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  304. /* Clear the capabilities if we switch to a non-root user */
  305. kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
  306. if (!uid_eq(override_cred->uid, root_uid))
  307. cap_clear(override_cred->cap_effective);
  308. else
  309. override_cred->cap_effective =
  310. override_cred->cap_permitted;
  311. }
  312. /*
  313. * The new set of credentials can *only* be used in
  314. * task-synchronous circumstances, and does not need
  315. * RCU freeing, unless somebody then takes a separate
  316. * reference to it.
  317. *
  318. * NOTE! This is _only_ true because this credential
  319. * is used purely for override_creds() that installs
  320. * it as the subjective cred. Other threads will be
  321. * accessing ->real_cred, not the subjective cred.
  322. *
  323. * If somebody _does_ make a copy of this (using the
  324. * 'get_current_cred()' function), that will clear the
  325. * non_rcu field, because now that other user may be
  326. * expecting RCU freeing. But normal thread-synchronous
  327. * cred accesses will keep things non-RCY.
  328. */
  329. override_cred->non_rcu = 1;
  330. old_cred = override_creds(override_cred);
  331. retry:
  332. res = user_path_at(dfd, filename, lookup_flags, &path);
  333. if (res)
  334. goto out;
  335. inode = d_backing_inode(path.dentry);
  336. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  337. /*
  338. * MAY_EXEC on regular files is denied if the fs is mounted
  339. * with the "noexec" flag.
  340. */
  341. res = -EACCES;
  342. if (path_noexec(&path))
  343. goto out_path_release;
  344. }
  345. res = inode_permission(inode, mode | MAY_ACCESS);
  346. /* SuS v2 requires we report a read only fs too */
  347. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  348. goto out_path_release;
  349. /*
  350. * This is a rare case where using __mnt_is_readonly()
  351. * is OK without a mnt_want/drop_write() pair. Since
  352. * no actual write to the fs is performed here, we do
  353. * not need to telegraph to that to anyone.
  354. *
  355. * By doing this, we accept that this access is
  356. * inherently racy and know that the fs may change
  357. * state before we even see this result.
  358. */
  359. if (__mnt_is_readonly(path.mnt))
  360. res = -EROFS;
  361. out_path_release:
  362. path_put(&path);
  363. if (retry_estale(res, lookup_flags)) {
  364. lookup_flags |= LOOKUP_REVAL;
  365. goto retry;
  366. }
  367. out:
  368. revert_creds(old_cred);
  369. put_cred(override_cred);
  370. return res;
  371. }
  372. SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
  373. {
  374. return do_faccessat(dfd, filename, mode);
  375. }
  376. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  377. {
  378. return do_faccessat(AT_FDCWD, filename, mode);
  379. }
  380. int ksys_chdir(const char __user *filename)
  381. {
  382. struct path path;
  383. int error;
  384. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  385. retry:
  386. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  387. if (error)
  388. goto out;
  389. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  390. if (error)
  391. goto dput_and_out;
  392. set_fs_pwd(current->fs, &path);
  393. dput_and_out:
  394. path_put(&path);
  395. if (retry_estale(error, lookup_flags)) {
  396. lookup_flags |= LOOKUP_REVAL;
  397. goto retry;
  398. }
  399. out:
  400. return error;
  401. }
  402. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  403. {
  404. return ksys_chdir(filename);
  405. }
  406. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  407. {
  408. struct fd f = fdget_raw(fd);
  409. int error;
  410. error = -EBADF;
  411. if (!f.file)
  412. goto out;
  413. error = -ENOTDIR;
  414. if (!d_can_lookup(f.file->f_path.dentry))
  415. goto out_putf;
  416. error = inode_permission(file_inode(f.file), MAY_EXEC | MAY_CHDIR);
  417. if (!error)
  418. set_fs_pwd(current->fs, &f.file->f_path);
  419. out_putf:
  420. fdput(f);
  421. out:
  422. return error;
  423. }
  424. int ksys_chroot(const char __user *filename)
  425. {
  426. struct path path;
  427. int error;
  428. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  429. retry:
  430. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  431. if (error)
  432. goto out;
  433. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  434. if (error)
  435. goto dput_and_out;
  436. error = -EPERM;
  437. if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
  438. goto dput_and_out;
  439. error = security_path_chroot(&path);
  440. if (error)
  441. goto dput_and_out;
  442. set_fs_root(current->fs, &path);
  443. error = 0;
  444. dput_and_out:
  445. path_put(&path);
  446. if (retry_estale(error, lookup_flags)) {
  447. lookup_flags |= LOOKUP_REVAL;
  448. goto retry;
  449. }
  450. out:
  451. return error;
  452. }
  453. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  454. {
  455. return ksys_chroot(filename);
  456. }
  457. static int chmod_common(const struct path *path, umode_t mode)
  458. {
  459. struct inode *inode = path->dentry->d_inode;
  460. struct inode *delegated_inode = NULL;
  461. struct iattr newattrs;
  462. int error;
  463. error = mnt_want_write(path->mnt);
  464. if (error)
  465. return error;
  466. retry_deleg:
  467. inode_lock(inode);
  468. error = security_path_chmod(path, mode);
  469. if (error)
  470. goto out_unlock;
  471. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  472. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  473. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  474. out_unlock:
  475. inode_unlock(inode);
  476. if (delegated_inode) {
  477. error = break_deleg_wait(&delegated_inode);
  478. if (!error)
  479. goto retry_deleg;
  480. }
  481. mnt_drop_write(path->mnt);
  482. return error;
  483. }
  484. int ksys_fchmod(unsigned int fd, umode_t mode)
  485. {
  486. struct fd f = fdget(fd);
  487. int err = -EBADF;
  488. if (f.file) {
  489. audit_file(f.file);
  490. err = chmod_common(&f.file->f_path, mode);
  491. fdput(f);
  492. }
  493. return err;
  494. }
  495. SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
  496. {
  497. return ksys_fchmod(fd, mode);
  498. }
  499. int do_fchmodat(int dfd, const char __user *filename, umode_t mode)
  500. {
  501. struct path path;
  502. int error;
  503. unsigned int lookup_flags = LOOKUP_FOLLOW;
  504. retry:
  505. error = user_path_at(dfd, filename, lookup_flags, &path);
  506. if (!error) {
  507. error = chmod_common(&path, mode);
  508. path_put(&path);
  509. if (retry_estale(error, lookup_flags)) {
  510. lookup_flags |= LOOKUP_REVAL;
  511. goto retry;
  512. }
  513. }
  514. return error;
  515. }
  516. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename,
  517. umode_t, mode)
  518. {
  519. return do_fchmodat(dfd, filename, mode);
  520. }
  521. SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
  522. {
  523. return do_fchmodat(AT_FDCWD, filename, mode);
  524. }
  525. static int chown_common(const struct path *path, uid_t user, gid_t group)
  526. {
  527. struct inode *inode = path->dentry->d_inode;
  528. struct inode *delegated_inode = NULL;
  529. int error;
  530. struct iattr newattrs;
  531. kuid_t uid;
  532. kgid_t gid;
  533. uid = make_kuid(current_user_ns(), user);
  534. gid = make_kgid(current_user_ns(), group);
  535. retry_deleg:
  536. newattrs.ia_valid = ATTR_CTIME;
  537. if (user != (uid_t) -1) {
  538. if (!uid_valid(uid))
  539. return -EINVAL;
  540. newattrs.ia_valid |= ATTR_UID;
  541. newattrs.ia_uid = uid;
  542. }
  543. if (group != (gid_t) -1) {
  544. if (!gid_valid(gid))
  545. return -EINVAL;
  546. newattrs.ia_valid |= ATTR_GID;
  547. newattrs.ia_gid = gid;
  548. }
  549. if (!S_ISDIR(inode->i_mode))
  550. newattrs.ia_valid |=
  551. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  552. inode_lock(inode);
  553. error = security_path_chown(path, uid, gid);
  554. if (!error)
  555. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  556. inode_unlock(inode);
  557. if (delegated_inode) {
  558. error = break_deleg_wait(&delegated_inode);
  559. if (!error)
  560. goto retry_deleg;
  561. }
  562. return error;
  563. }
  564. int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
  565. int flag)
  566. {
  567. struct path path;
  568. int error = -EINVAL;
  569. int lookup_flags;
  570. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  571. goto out;
  572. lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  573. if (flag & AT_EMPTY_PATH)
  574. lookup_flags |= LOOKUP_EMPTY;
  575. retry:
  576. error = user_path_at(dfd, filename, lookup_flags, &path);
  577. if (error)
  578. goto out;
  579. error = mnt_want_write(path.mnt);
  580. if (error)
  581. goto out_release;
  582. error = chown_common(&path, user, group);
  583. mnt_drop_write(path.mnt);
  584. out_release:
  585. path_put(&path);
  586. if (retry_estale(error, lookup_flags)) {
  587. lookup_flags |= LOOKUP_REVAL;
  588. goto retry;
  589. }
  590. out:
  591. return error;
  592. }
  593. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  594. gid_t, group, int, flag)
  595. {
  596. return do_fchownat(dfd, filename, user, group, flag);
  597. }
  598. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  599. {
  600. return do_fchownat(AT_FDCWD, filename, user, group, 0);
  601. }
  602. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  603. {
  604. return do_fchownat(AT_FDCWD, filename, user, group,
  605. AT_SYMLINK_NOFOLLOW);
  606. }
  607. int ksys_fchown(unsigned int fd, uid_t user, gid_t group)
  608. {
  609. struct fd f = fdget(fd);
  610. int error = -EBADF;
  611. if (!f.file)
  612. goto out;
  613. error = mnt_want_write_file(f.file);
  614. if (error)
  615. goto out_fput;
  616. audit_file(f.file);
  617. error = chown_common(&f.file->f_path, user, group);
  618. mnt_drop_write_file(f.file);
  619. out_fput:
  620. fdput(f);
  621. out:
  622. return error;
  623. }
  624. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  625. {
  626. return ksys_fchown(fd, user, group);
  627. }
  628. static int do_dentry_open(struct file *f,
  629. struct inode *inode,
  630. int (*open)(struct inode *, struct file *))
  631. {
  632. static const struct file_operations empty_fops = {};
  633. int error;
  634. path_get(&f->f_path);
  635. f->f_inode = inode;
  636. f->f_mapping = inode->i_mapping;
  637. /* Ensure that we skip any errors that predate opening of the file */
  638. f->f_wb_err = filemap_sample_wb_err(f->f_mapping);
  639. if (unlikely(f->f_flags & O_PATH)) {
  640. f->f_mode = FMODE_PATH | FMODE_OPENED;
  641. f->f_op = &empty_fops;
  642. return 0;
  643. }
  644. /* Any file opened for execve()/uselib() has to be a regular file. */
  645. if (unlikely(f->f_flags & FMODE_EXEC && !S_ISREG(inode->i_mode))) {
  646. error = -EACCES;
  647. goto cleanup_file;
  648. }
  649. if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
  650. error = get_write_access(inode);
  651. if (unlikely(error))
  652. goto cleanup_file;
  653. error = __mnt_want_write(f->f_path.mnt);
  654. if (unlikely(error)) {
  655. put_write_access(inode);
  656. goto cleanup_file;
  657. }
  658. f->f_mode |= FMODE_WRITER;
  659. }
  660. /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
  661. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
  662. f->f_mode |= FMODE_ATOMIC_POS;
  663. f->f_op = fops_get(inode->i_fop);
  664. if (unlikely(WARN_ON(!f->f_op))) {
  665. error = -ENODEV;
  666. goto cleanup_all;
  667. }
  668. error = security_file_open(f);
  669. if (error)
  670. goto cleanup_all;
  671. error = break_lease(locks_inode(f), f->f_flags);
  672. if (error)
  673. goto cleanup_all;
  674. /* normally all 3 are set; ->open() can clear them if needed */
  675. f->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
  676. if (!open)
  677. open = f->f_op->open;
  678. if (open) {
  679. error = open(inode, f);
  680. if (error)
  681. goto cleanup_all;
  682. }
  683. f->f_mode |= FMODE_OPENED;
  684. if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  685. i_readcount_inc(inode);
  686. if ((f->f_mode & FMODE_READ) &&
  687. likely(f->f_op->read || f->f_op->read_iter))
  688. f->f_mode |= FMODE_CAN_READ;
  689. if ((f->f_mode & FMODE_WRITE) &&
  690. likely(f->f_op->write || f->f_op->write_iter))
  691. f->f_mode |= FMODE_CAN_WRITE;
  692. f->f_write_hint = WRITE_LIFE_NOT_SET;
  693. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  694. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  695. /* NB: we're sure to have correct a_ops only after f_op->open */
  696. if (f->f_flags & O_DIRECT) {
  697. if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
  698. return -EINVAL;
  699. }
  700. return 0;
  701. cleanup_all:
  702. if (WARN_ON_ONCE(error > 0))
  703. error = -EINVAL;
  704. fops_put(f->f_op);
  705. if (f->f_mode & FMODE_WRITER) {
  706. put_write_access(inode);
  707. __mnt_drop_write(f->f_path.mnt);
  708. }
  709. cleanup_file:
  710. path_put(&f->f_path);
  711. f->f_path.mnt = NULL;
  712. f->f_path.dentry = NULL;
  713. f->f_inode = NULL;
  714. return error;
  715. }
  716. /**
  717. * finish_open - finish opening a file
  718. * @file: file pointer
  719. * @dentry: pointer to dentry
  720. * @open: open callback
  721. * @opened: state of open
  722. *
  723. * This can be used to finish opening a file passed to i_op->atomic_open().
  724. *
  725. * If the open callback is set to NULL, then the standard f_op->open()
  726. * filesystem callback is substituted.
  727. *
  728. * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
  729. * the return value of d_splice_alias(), then the caller needs to perform dput()
  730. * on it after finish_open().
  731. *
  732. * Returns zero on success or -errno if the open failed.
  733. */
  734. int finish_open(struct file *file, struct dentry *dentry,
  735. int (*open)(struct inode *, struct file *))
  736. {
  737. BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
  738. file->f_path.dentry = dentry;
  739. return do_dentry_open(file, d_backing_inode(dentry), open);
  740. }
  741. EXPORT_SYMBOL(finish_open);
  742. /**
  743. * finish_no_open - finish ->atomic_open() without opening the file
  744. *
  745. * @file: file pointer
  746. * @dentry: dentry or NULL (as returned from ->lookup())
  747. *
  748. * This can be used to set the result of a successful lookup in ->atomic_open().
  749. *
  750. * NB: unlike finish_open() this function does consume the dentry reference and
  751. * the caller need not dput() it.
  752. *
  753. * Returns "0" which must be the return value of ->atomic_open() after having
  754. * called this function.
  755. */
  756. int finish_no_open(struct file *file, struct dentry *dentry)
  757. {
  758. file->f_path.dentry = dentry;
  759. return 0;
  760. }
  761. EXPORT_SYMBOL(finish_no_open);
  762. char *file_path(struct file *filp, char *buf, int buflen)
  763. {
  764. return d_path(&filp->f_path, buf, buflen);
  765. }
  766. EXPORT_SYMBOL(file_path);
  767. /**
  768. * vfs_open - open the file at the given path
  769. * @path: path to open
  770. * @file: newly allocated file with f_flag initialized
  771. * @cred: credentials to use
  772. */
  773. int vfs_open(const struct path *path, struct file *file)
  774. {
  775. file->f_path = *path;
  776. return do_dentry_open(file, d_backing_inode(path->dentry), NULL);
  777. }
  778. struct file *dentry_open(const struct path *path, int flags,
  779. const struct cred *cred)
  780. {
  781. int error;
  782. struct file *f;
  783. validate_creds(cred);
  784. /* We must always pass in a valid mount pointer. */
  785. BUG_ON(!path->mnt);
  786. f = alloc_empty_file(flags, cred);
  787. if (!IS_ERR(f)) {
  788. error = vfs_open(path, f);
  789. if (error) {
  790. fput(f);
  791. f = ERR_PTR(error);
  792. }
  793. }
  794. return f;
  795. }
  796. EXPORT_SYMBOL(dentry_open);
  797. struct file *open_with_fake_path(const struct path *path, int flags,
  798. struct inode *inode, const struct cred *cred)
  799. {
  800. struct file *f = alloc_empty_file_noaccount(flags, cred);
  801. if (!IS_ERR(f)) {
  802. int error;
  803. f->f_path = *path;
  804. error = do_dentry_open(f, inode, NULL);
  805. if (error) {
  806. fput(f);
  807. f = ERR_PTR(error);
  808. }
  809. }
  810. return f;
  811. }
  812. EXPORT_SYMBOL(open_with_fake_path);
  813. static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
  814. {
  815. int lookup_flags = 0;
  816. int acc_mode = ACC_MODE(flags);
  817. /*
  818. * Clear out all open flags we don't know about so that we don't report
  819. * them in fcntl(F_GETFD) or similar interfaces.
  820. */
  821. flags &= VALID_OPEN_FLAGS;
  822. if (flags & (O_CREAT | __O_TMPFILE))
  823. op->mode = (mode & S_IALLUGO) | S_IFREG;
  824. else
  825. op->mode = 0;
  826. /* Must never be set by userspace */
  827. flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
  828. /*
  829. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  830. * check for O_DSYNC if the need any syncing at all we enforce it's
  831. * always set instead of having to deal with possibly weird behaviour
  832. * for malicious applications setting only __O_SYNC.
  833. */
  834. if (flags & __O_SYNC)
  835. flags |= O_DSYNC;
  836. if (flags & __O_TMPFILE) {
  837. if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
  838. return -EINVAL;
  839. if (!(acc_mode & MAY_WRITE))
  840. return -EINVAL;
  841. } else if (flags & O_PATH) {
  842. /*
  843. * If we have O_PATH in the open flag. Then we
  844. * cannot have anything other than the below set of flags
  845. */
  846. flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
  847. acc_mode = 0;
  848. }
  849. op->open_flag = flags;
  850. /* O_TRUNC implies we need access checks for write permissions */
  851. if (flags & O_TRUNC)
  852. acc_mode |= MAY_WRITE;
  853. /* Allow the LSM permission hook to distinguish append
  854. access from general write access. */
  855. if (flags & O_APPEND)
  856. acc_mode |= MAY_APPEND;
  857. op->acc_mode = acc_mode;
  858. op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
  859. if (flags & O_CREAT) {
  860. op->intent |= LOOKUP_CREATE;
  861. if (flags & O_EXCL)
  862. op->intent |= LOOKUP_EXCL;
  863. }
  864. if (flags & O_DIRECTORY)
  865. lookup_flags |= LOOKUP_DIRECTORY;
  866. if (!(flags & O_NOFOLLOW))
  867. lookup_flags |= LOOKUP_FOLLOW;
  868. op->lookup_flags = lookup_flags;
  869. return 0;
  870. }
  871. /**
  872. * file_open_name - open file and return file pointer
  873. *
  874. * @name: struct filename containing path to open
  875. * @flags: open flags as per the open(2) second argument
  876. * @mode: mode for the new file if O_CREAT is set, else ignored
  877. *
  878. * This is the helper to open a file from kernelspace if you really
  879. * have to. But in generally you should not do this, so please move
  880. * along, nothing to see here..
  881. */
  882. struct file *file_open_name(struct filename *name, int flags, umode_t mode)
  883. {
  884. struct open_flags op;
  885. int err = build_open_flags(flags, mode, &op);
  886. return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
  887. }
  888. /**
  889. * filp_open - open file and return file pointer
  890. *
  891. * @filename: path to open
  892. * @flags: open flags as per the open(2) second argument
  893. * @mode: mode for the new file if O_CREAT is set, else ignored
  894. *
  895. * This is the helper to open a file from kernelspace if you really
  896. * have to. But in generally you should not do this, so please move
  897. * along, nothing to see here..
  898. */
  899. struct file *filp_open(const char *filename, int flags, umode_t mode)
  900. {
  901. struct filename *name = getname_kernel(filename);
  902. struct file *file = ERR_CAST(name);
  903. if (!IS_ERR(name)) {
  904. file = file_open_name(name, flags, mode);
  905. putname(name);
  906. }
  907. return file;
  908. }
  909. EXPORT_SYMBOL(filp_open);
  910. struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  911. const char *filename, int flags, umode_t mode)
  912. {
  913. struct open_flags op;
  914. int err = build_open_flags(flags, mode, &op);
  915. if (err)
  916. return ERR_PTR(err);
  917. return do_file_open_root(dentry, mnt, filename, &op);
  918. }
  919. EXPORT_SYMBOL(file_open_root);
  920. long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
  921. {
  922. struct open_flags op;
  923. int fd = build_open_flags(flags, mode, &op);
  924. struct filename *tmp;
  925. if (fd)
  926. return fd;
  927. tmp = getname(filename);
  928. if (IS_ERR(tmp))
  929. return PTR_ERR(tmp);
  930. fd = get_unused_fd_flags(flags);
  931. if (fd >= 0) {
  932. struct file *f = do_filp_open(dfd, tmp, &op);
  933. if (IS_ERR(f)) {
  934. put_unused_fd(fd);
  935. fd = PTR_ERR(f);
  936. } else {
  937. fsnotify_open(f);
  938. fd_install(fd, f);
  939. }
  940. }
  941. putname(tmp);
  942. return fd;
  943. }
  944. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  945. {
  946. if (force_o_largefile())
  947. flags |= O_LARGEFILE;
  948. return do_sys_open(AT_FDCWD, filename, flags, mode);
  949. }
  950. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  951. umode_t, mode)
  952. {
  953. if (force_o_largefile())
  954. flags |= O_LARGEFILE;
  955. return do_sys_open(dfd, filename, flags, mode);
  956. }
  957. #ifdef CONFIG_COMPAT
  958. /*
  959. * Exactly like sys_open(), except that it doesn't set the
  960. * O_LARGEFILE flag.
  961. */
  962. COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  963. {
  964. return do_sys_open(AT_FDCWD, filename, flags, mode);
  965. }
  966. /*
  967. * Exactly like sys_openat(), except that it doesn't set the
  968. * O_LARGEFILE flag.
  969. */
  970. COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
  971. {
  972. return do_sys_open(dfd, filename, flags, mode);
  973. }
  974. #endif
  975. #ifndef __alpha__
  976. /*
  977. * For backward compatibility? Maybe this should be moved
  978. * into arch/i386 instead?
  979. */
  980. SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
  981. {
  982. return ksys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  983. }
  984. #endif
  985. /*
  986. * "id" is the POSIX thread ID. We use the
  987. * files pointer for this..
  988. */
  989. int filp_close(struct file *filp, fl_owner_t id)
  990. {
  991. int retval = 0;
  992. if (!file_count(filp)) {
  993. printk(KERN_ERR "VFS: Close: file count is 0\n");
  994. return 0;
  995. }
  996. if (filp->f_op->flush)
  997. retval = filp->f_op->flush(filp, id);
  998. if (likely(!(filp->f_mode & FMODE_PATH))) {
  999. dnotify_flush(filp, id);
  1000. locks_remove_posix(filp, id);
  1001. }
  1002. fput(filp);
  1003. return retval;
  1004. }
  1005. EXPORT_SYMBOL(filp_close);
  1006. /*
  1007. * Careful here! We test whether the file pointer is NULL before
  1008. * releasing the fd. This ensures that one clone task can't release
  1009. * an fd while another clone is opening it.
  1010. */
  1011. SYSCALL_DEFINE1(close, unsigned int, fd)
  1012. {
  1013. int retval = __close_fd(current->files, fd);
  1014. /* can't restart close syscall because file table entry was cleared */
  1015. if (unlikely(retval == -ERESTARTSYS ||
  1016. retval == -ERESTARTNOINTR ||
  1017. retval == -ERESTARTNOHAND ||
  1018. retval == -ERESTART_RESTARTBLOCK))
  1019. retval = -EINTR;
  1020. return retval;
  1021. }
  1022. /*
  1023. * This routine simulates a hangup on the tty, to arrange that users
  1024. * are given clean terminals at login time.
  1025. */
  1026. SYSCALL_DEFINE0(vhangup)
  1027. {
  1028. if (capable(CAP_SYS_TTY_CONFIG)) {
  1029. tty_vhangup_self();
  1030. return 0;
  1031. }
  1032. return -EPERM;
  1033. }
  1034. /*
  1035. * Called when an inode is about to be open.
  1036. * We use this to disallow opening large files on 32bit systems if
  1037. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  1038. * on this flag in sys_open.
  1039. */
  1040. int generic_file_open(struct inode * inode, struct file * filp)
  1041. {
  1042. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  1043. return -EOVERFLOW;
  1044. return 0;
  1045. }
  1046. EXPORT_SYMBOL(generic_file_open);
  1047. /*
  1048. * This is used by subsystems that don't want seekable
  1049. * file descriptors. The function is not supposed to ever fail, the only
  1050. * reason it returns an 'int' and not 'void' is so that it can be plugged
  1051. * directly into file_operations structure.
  1052. */
  1053. int nonseekable_open(struct inode *inode, struct file *filp)
  1054. {
  1055. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1056. return 0;
  1057. }
  1058. EXPORT_SYMBOL(nonseekable_open);
  1059. /*
  1060. * stream_open is used by subsystems that want stream-like file descriptors.
  1061. * Such file descriptors are not seekable and don't have notion of position
  1062. * (file.f_pos is always 0). Contrary to file descriptors of other regular
  1063. * files, .read() and .write() can run simultaneously.
  1064. *
  1065. * stream_open never fails and is marked to return int so that it could be
  1066. * directly used as file_operations.open .
  1067. */
  1068. int stream_open(struct inode *inode, struct file *filp)
  1069. {
  1070. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS);
  1071. filp->f_mode |= FMODE_STREAM;
  1072. return 0;
  1073. }
  1074. EXPORT_SYMBOL(stream_open);