open.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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 <asm/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. mutex_lock(&dentry->d_inode->i_mutex);
  55. /* Note any delegations or leases have already been broken: */
  56. ret = notify_change(dentry, &newattrs, NULL);
  57. mutex_unlock(&dentry->d_inode->i_mutex);
  58. return ret;
  59. }
  60. long vfs_truncate(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. static 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. static 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. if (IS_APPEND(inode))
  158. goto out_putf;
  159. sb_start_write(inode->i_sb);
  160. error = locks_verify_truncate(inode, f.file, length);
  161. if (!error)
  162. error = security_path_truncate(&f.file->f_path);
  163. if (!error)
  164. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
  165. sb_end_write(inode->i_sb);
  166. out_putf:
  167. fdput(f);
  168. out:
  169. return error;
  170. }
  171. SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
  172. {
  173. return do_sys_ftruncate(fd, length, 1);
  174. }
  175. #ifdef CONFIG_COMPAT
  176. COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
  177. {
  178. return do_sys_ftruncate(fd, length, 1);
  179. }
  180. #endif
  181. /* LFS versions of truncate are only needed on 32 bit machines */
  182. #if BITS_PER_LONG == 32
  183. SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
  184. {
  185. return do_sys_truncate(path, length);
  186. }
  187. SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
  188. {
  189. return do_sys_ftruncate(fd, length, 0);
  190. }
  191. #endif /* BITS_PER_LONG == 32 */
  192. int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  193. {
  194. struct inode *inode = file_inode(file);
  195. long ret;
  196. if (offset < 0 || len <= 0)
  197. return -EINVAL;
  198. /* Return error if mode is not supported */
  199. if (mode & ~FALLOC_FL_SUPPORTED_MASK)
  200. return -EOPNOTSUPP;
  201. /* Punch hole and zero range are mutually exclusive */
  202. if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
  203. (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
  204. return -EOPNOTSUPP;
  205. /* Punch hole must have keep size set */
  206. if ((mode & FALLOC_FL_PUNCH_HOLE) &&
  207. !(mode & FALLOC_FL_KEEP_SIZE))
  208. return -EOPNOTSUPP;
  209. /* Collapse range should only be used exclusively. */
  210. if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
  211. (mode & ~FALLOC_FL_COLLAPSE_RANGE))
  212. return -EINVAL;
  213. /* Insert range should only be used exclusively. */
  214. if ((mode & FALLOC_FL_INSERT_RANGE) &&
  215. (mode & ~FALLOC_FL_INSERT_RANGE))
  216. return -EINVAL;
  217. if (!(file->f_mode & FMODE_WRITE))
  218. return -EBADF;
  219. /*
  220. * We can only allow pure fallocate on append only files
  221. */
  222. if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
  223. return -EPERM;
  224. if (IS_IMMUTABLE(inode))
  225. return -EPERM;
  226. /*
  227. * We cannot allow any fallocate operation on an active swapfile
  228. */
  229. if (IS_SWAPFILE(inode))
  230. return -ETXTBSY;
  231. /*
  232. * Revalidate the write permissions, in case security policy has
  233. * changed since the files were opened.
  234. */
  235. ret = security_file_permission(file, MAY_WRITE);
  236. if (ret)
  237. return ret;
  238. if (S_ISFIFO(inode->i_mode))
  239. return -ESPIPE;
  240. /*
  241. * Let individual file system decide if it supports preallocation
  242. * for directories or not.
  243. */
  244. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  245. return -ENODEV;
  246. /* Check for wrap through zero too */
  247. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  248. return -EFBIG;
  249. if (!file->f_op->fallocate)
  250. return -EOPNOTSUPP;
  251. sb_start_write(inode->i_sb);
  252. ret = file->f_op->fallocate(file, mode, offset, len);
  253. /*
  254. * Create inotify and fanotify events.
  255. *
  256. * To keep the logic simple always create events if fallocate succeeds.
  257. * This implies that events are even created if the file size remains
  258. * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
  259. */
  260. if (ret == 0)
  261. fsnotify_modify(file);
  262. sb_end_write(inode->i_sb);
  263. return ret;
  264. }
  265. EXPORT_SYMBOL_GPL(vfs_fallocate);
  266. SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
  267. {
  268. struct fd f = fdget(fd);
  269. int error = -EBADF;
  270. if (f.file) {
  271. error = vfs_fallocate(f.file, mode, offset, len);
  272. fdput(f);
  273. }
  274. return error;
  275. }
  276. /*
  277. * access() needs to use the real uid/gid, not the effective uid/gid.
  278. * We do this by temporarily clearing all FS-related capabilities and
  279. * switching the fsuid/fsgid around to the real ones.
  280. */
  281. SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
  282. {
  283. const struct cred *old_cred;
  284. struct cred *override_cred;
  285. struct path path;
  286. struct inode *inode;
  287. int res;
  288. unsigned int lookup_flags = LOOKUP_FOLLOW;
  289. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  290. return -EINVAL;
  291. override_cred = prepare_creds();
  292. if (!override_cred)
  293. return -ENOMEM;
  294. override_cred->fsuid = override_cred->uid;
  295. override_cred->fsgid = override_cred->gid;
  296. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  297. /* Clear the capabilities if we switch to a non-root user */
  298. kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
  299. if (!uid_eq(override_cred->uid, root_uid))
  300. cap_clear(override_cred->cap_effective);
  301. else
  302. override_cred->cap_effective =
  303. override_cred->cap_permitted;
  304. }
  305. old_cred = override_creds(override_cred);
  306. retry:
  307. res = user_path_at(dfd, filename, lookup_flags, &path);
  308. if (res)
  309. goto out;
  310. inode = d_backing_inode(path.dentry);
  311. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  312. /*
  313. * MAY_EXEC on regular files is denied if the fs is mounted
  314. * with the "noexec" flag.
  315. */
  316. res = -EACCES;
  317. if (path.mnt->mnt_flags & MNT_NOEXEC)
  318. goto out_path_release;
  319. }
  320. res = inode_permission(inode, mode | MAY_ACCESS);
  321. /* SuS v2 requires we report a read only fs too */
  322. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  323. goto out_path_release;
  324. /*
  325. * This is a rare case where using __mnt_is_readonly()
  326. * is OK without a mnt_want/drop_write() pair. Since
  327. * no actual write to the fs is performed here, we do
  328. * not need to telegraph to that to anyone.
  329. *
  330. * By doing this, we accept that this access is
  331. * inherently racy and know that the fs may change
  332. * state before we even see this result.
  333. */
  334. if (__mnt_is_readonly(path.mnt))
  335. res = -EROFS;
  336. out_path_release:
  337. path_put(&path);
  338. if (retry_estale(res, lookup_flags)) {
  339. lookup_flags |= LOOKUP_REVAL;
  340. goto retry;
  341. }
  342. out:
  343. revert_creds(old_cred);
  344. put_cred(override_cred);
  345. return res;
  346. }
  347. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  348. {
  349. return sys_faccessat(AT_FDCWD, filename, mode);
  350. }
  351. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  352. {
  353. struct path path;
  354. int error;
  355. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  356. retry:
  357. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  358. if (error)
  359. goto out;
  360. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  361. if (error)
  362. goto dput_and_out;
  363. set_fs_pwd(current->fs, &path);
  364. dput_and_out:
  365. path_put(&path);
  366. if (retry_estale(error, lookup_flags)) {
  367. lookup_flags |= LOOKUP_REVAL;
  368. goto retry;
  369. }
  370. out:
  371. return error;
  372. }
  373. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  374. {
  375. struct fd f = fdget_raw(fd);
  376. struct inode *inode;
  377. int error = -EBADF;
  378. error = -EBADF;
  379. if (!f.file)
  380. goto out;
  381. inode = file_inode(f.file);
  382. error = -ENOTDIR;
  383. if (!S_ISDIR(inode->i_mode))
  384. goto out_putf;
  385. error = inode_permission(inode, MAY_EXEC | MAY_CHDIR);
  386. if (!error)
  387. set_fs_pwd(current->fs, &f.file->f_path);
  388. out_putf:
  389. fdput(f);
  390. out:
  391. return error;
  392. }
  393. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  394. {
  395. struct path path;
  396. int error;
  397. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  398. retry:
  399. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  400. if (error)
  401. goto out;
  402. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  403. if (error)
  404. goto dput_and_out;
  405. error = -EPERM;
  406. if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
  407. goto dput_and_out;
  408. error = security_path_chroot(&path);
  409. if (error)
  410. goto dput_and_out;
  411. set_fs_root(current->fs, &path);
  412. error = 0;
  413. dput_and_out:
  414. path_put(&path);
  415. if (retry_estale(error, lookup_flags)) {
  416. lookup_flags |= LOOKUP_REVAL;
  417. goto retry;
  418. }
  419. out:
  420. return error;
  421. }
  422. static int chmod_common(struct path *path, umode_t mode)
  423. {
  424. struct inode *inode = path->dentry->d_inode;
  425. struct inode *delegated_inode = NULL;
  426. struct iattr newattrs;
  427. int error;
  428. error = mnt_want_write(path->mnt);
  429. if (error)
  430. return error;
  431. retry_deleg:
  432. mutex_lock(&inode->i_mutex);
  433. error = security_path_chmod(path, mode);
  434. if (error)
  435. goto out_unlock;
  436. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  437. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  438. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  439. out_unlock:
  440. mutex_unlock(&inode->i_mutex);
  441. if (delegated_inode) {
  442. error = break_deleg_wait(&delegated_inode);
  443. if (!error)
  444. goto retry_deleg;
  445. }
  446. mnt_drop_write(path->mnt);
  447. return error;
  448. }
  449. SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
  450. {
  451. struct fd f = fdget(fd);
  452. int err = -EBADF;
  453. if (f.file) {
  454. audit_file(f.file);
  455. err = chmod_common(&f.file->f_path, mode);
  456. fdput(f);
  457. }
  458. return err;
  459. }
  460. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
  461. {
  462. struct path path;
  463. int error;
  464. unsigned int lookup_flags = LOOKUP_FOLLOW;
  465. retry:
  466. error = user_path_at(dfd, filename, lookup_flags, &path);
  467. if (!error) {
  468. error = chmod_common(&path, mode);
  469. path_put(&path);
  470. if (retry_estale(error, lookup_flags)) {
  471. lookup_flags |= LOOKUP_REVAL;
  472. goto retry;
  473. }
  474. }
  475. return error;
  476. }
  477. SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
  478. {
  479. return sys_fchmodat(AT_FDCWD, filename, mode);
  480. }
  481. static int chown_common(struct path *path, uid_t user, gid_t group)
  482. {
  483. struct inode *inode = path->dentry->d_inode;
  484. struct inode *delegated_inode = NULL;
  485. int error;
  486. struct iattr newattrs;
  487. kuid_t uid;
  488. kgid_t gid;
  489. uid = make_kuid(current_user_ns(), user);
  490. gid = make_kgid(current_user_ns(), group);
  491. retry_deleg:
  492. newattrs.ia_valid = ATTR_CTIME;
  493. if (user != (uid_t) -1) {
  494. if (!uid_valid(uid))
  495. return -EINVAL;
  496. newattrs.ia_valid |= ATTR_UID;
  497. newattrs.ia_uid = uid;
  498. }
  499. if (group != (gid_t) -1) {
  500. if (!gid_valid(gid))
  501. return -EINVAL;
  502. newattrs.ia_valid |= ATTR_GID;
  503. newattrs.ia_gid = gid;
  504. }
  505. if (!S_ISDIR(inode->i_mode))
  506. newattrs.ia_valid |=
  507. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  508. mutex_lock(&inode->i_mutex);
  509. error = security_path_chown(path, uid, gid);
  510. if (!error)
  511. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  512. mutex_unlock(&inode->i_mutex);
  513. if (delegated_inode) {
  514. error = break_deleg_wait(&delegated_inode);
  515. if (!error)
  516. goto retry_deleg;
  517. }
  518. return error;
  519. }
  520. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  521. gid_t, group, int, flag)
  522. {
  523. struct path path;
  524. int error = -EINVAL;
  525. int lookup_flags;
  526. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  527. goto out;
  528. lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  529. if (flag & AT_EMPTY_PATH)
  530. lookup_flags |= LOOKUP_EMPTY;
  531. retry:
  532. error = user_path_at(dfd, filename, lookup_flags, &path);
  533. if (error)
  534. goto out;
  535. error = mnt_want_write(path.mnt);
  536. if (error)
  537. goto out_release;
  538. error = chown_common(&path, user, group);
  539. mnt_drop_write(path.mnt);
  540. out_release:
  541. path_put(&path);
  542. if (retry_estale(error, lookup_flags)) {
  543. lookup_flags |= LOOKUP_REVAL;
  544. goto retry;
  545. }
  546. out:
  547. return error;
  548. }
  549. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  550. {
  551. return sys_fchownat(AT_FDCWD, filename, user, group, 0);
  552. }
  553. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  554. {
  555. return sys_fchownat(AT_FDCWD, filename, user, group,
  556. AT_SYMLINK_NOFOLLOW);
  557. }
  558. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  559. {
  560. struct fd f = fdget(fd);
  561. int error = -EBADF;
  562. if (!f.file)
  563. goto out;
  564. error = mnt_want_write_file(f.file);
  565. if (error)
  566. goto out_fput;
  567. audit_file(f.file);
  568. error = chown_common(&f.file->f_path, user, group);
  569. mnt_drop_write_file(f.file);
  570. out_fput:
  571. fdput(f);
  572. out:
  573. return error;
  574. }
  575. int open_check_o_direct(struct file *f)
  576. {
  577. /* NB: we're sure to have correct a_ops only after f_op->open */
  578. if (f->f_flags & O_DIRECT) {
  579. if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
  580. return -EINVAL;
  581. }
  582. return 0;
  583. }
  584. static int do_dentry_open(struct file *f,
  585. struct inode *inode,
  586. int (*open)(struct inode *, struct file *),
  587. const struct cred *cred)
  588. {
  589. static const struct file_operations empty_fops = {};
  590. int error;
  591. f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
  592. FMODE_PREAD | FMODE_PWRITE;
  593. path_get(&f->f_path);
  594. f->f_inode = inode;
  595. f->f_mapping = inode->i_mapping;
  596. if (unlikely(f->f_flags & O_PATH)) {
  597. f->f_mode = FMODE_PATH;
  598. f->f_op = &empty_fops;
  599. return 0;
  600. }
  601. if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
  602. error = get_write_access(inode);
  603. if (unlikely(error))
  604. goto cleanup_file;
  605. error = __mnt_want_write(f->f_path.mnt);
  606. if (unlikely(error)) {
  607. put_write_access(inode);
  608. goto cleanup_file;
  609. }
  610. f->f_mode |= FMODE_WRITER;
  611. }
  612. /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
  613. if (S_ISREG(inode->i_mode))
  614. f->f_mode |= FMODE_ATOMIC_POS;
  615. f->f_op = fops_get(inode->i_fop);
  616. if (unlikely(WARN_ON(!f->f_op))) {
  617. error = -ENODEV;
  618. goto cleanup_all;
  619. }
  620. error = security_file_open(f, cred);
  621. if (error)
  622. goto cleanup_all;
  623. error = break_lease(inode, f->f_flags);
  624. if (error)
  625. goto cleanup_all;
  626. if (!open)
  627. open = f->f_op->open;
  628. if (open) {
  629. error = open(inode, f);
  630. if (error)
  631. goto cleanup_all;
  632. }
  633. if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  634. i_readcount_inc(inode);
  635. if ((f->f_mode & FMODE_READ) &&
  636. likely(f->f_op->read || f->f_op->read_iter))
  637. f->f_mode |= FMODE_CAN_READ;
  638. if ((f->f_mode & FMODE_WRITE) &&
  639. likely(f->f_op->write || f->f_op->write_iter))
  640. f->f_mode |= FMODE_CAN_WRITE;
  641. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  642. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  643. return 0;
  644. cleanup_all:
  645. fops_put(f->f_op);
  646. if (f->f_mode & FMODE_WRITER) {
  647. put_write_access(inode);
  648. __mnt_drop_write(f->f_path.mnt);
  649. }
  650. cleanup_file:
  651. path_put(&f->f_path);
  652. f->f_path.mnt = NULL;
  653. f->f_path.dentry = NULL;
  654. f->f_inode = NULL;
  655. return error;
  656. }
  657. /**
  658. * finish_open - finish opening a file
  659. * @file: file pointer
  660. * @dentry: pointer to dentry
  661. * @open: open callback
  662. * @opened: state of open
  663. *
  664. * This can be used to finish opening a file passed to i_op->atomic_open().
  665. *
  666. * If the open callback is set to NULL, then the standard f_op->open()
  667. * filesystem callback is substituted.
  668. *
  669. * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
  670. * the return value of d_splice_alias(), then the caller needs to perform dput()
  671. * on it after finish_open().
  672. *
  673. * On successful return @file is a fully instantiated open file. After this, if
  674. * an error occurs in ->atomic_open(), it needs to clean up with fput().
  675. *
  676. * Returns zero on success or -errno if the open failed.
  677. */
  678. int finish_open(struct file *file, struct dentry *dentry,
  679. int (*open)(struct inode *, struct file *),
  680. int *opened)
  681. {
  682. int error;
  683. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  684. file->f_path.dentry = dentry;
  685. error = do_dentry_open(file, d_backing_inode(dentry), open,
  686. current_cred());
  687. if (!error)
  688. *opened |= FILE_OPENED;
  689. return error;
  690. }
  691. EXPORT_SYMBOL(finish_open);
  692. /**
  693. * finish_no_open - finish ->atomic_open() without opening the file
  694. *
  695. * @file: file pointer
  696. * @dentry: dentry or NULL (as returned from ->lookup())
  697. *
  698. * This can be used to set the result of a successful lookup in ->atomic_open().
  699. *
  700. * NB: unlike finish_open() this function does consume the dentry reference and
  701. * the caller need not dput() it.
  702. *
  703. * Returns "1" which must be the return value of ->atomic_open() after having
  704. * called this function.
  705. */
  706. int finish_no_open(struct file *file, struct dentry *dentry)
  707. {
  708. file->f_path.dentry = dentry;
  709. return 1;
  710. }
  711. EXPORT_SYMBOL(finish_no_open);
  712. char *file_path(struct file *filp, char *buf, int buflen)
  713. {
  714. return d_path(&filp->f_path, buf, buflen);
  715. }
  716. EXPORT_SYMBOL(file_path);
  717. /**
  718. * vfs_open - open the file at the given path
  719. * @path: path to open
  720. * @file: newly allocated file with f_flag initialized
  721. * @cred: credentials to use
  722. */
  723. int vfs_open(const struct path *path, struct file *file,
  724. const struct cred *cred)
  725. {
  726. struct dentry *dentry = path->dentry;
  727. struct inode *inode = dentry->d_inode;
  728. file->f_path = *path;
  729. if (dentry->d_flags & DCACHE_OP_SELECT_INODE) {
  730. inode = dentry->d_op->d_select_inode(dentry, file->f_flags);
  731. if (IS_ERR(inode))
  732. return PTR_ERR(inode);
  733. }
  734. return do_dentry_open(file, inode, NULL, cred);
  735. }
  736. struct file *dentry_open(const struct path *path, int flags,
  737. const struct cred *cred)
  738. {
  739. int error;
  740. struct file *f;
  741. validate_creds(cred);
  742. /* We must always pass in a valid mount pointer. */
  743. BUG_ON(!path->mnt);
  744. f = get_empty_filp();
  745. if (!IS_ERR(f)) {
  746. f->f_flags = flags;
  747. error = vfs_open(path, f, cred);
  748. if (!error) {
  749. /* from now on we need fput() to dispose of f */
  750. error = open_check_o_direct(f);
  751. if (error) {
  752. fput(f);
  753. f = ERR_PTR(error);
  754. }
  755. } else {
  756. put_filp(f);
  757. f = ERR_PTR(error);
  758. }
  759. }
  760. return f;
  761. }
  762. EXPORT_SYMBOL(dentry_open);
  763. static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
  764. {
  765. int lookup_flags = 0;
  766. int acc_mode;
  767. if (flags & (O_CREAT | __O_TMPFILE))
  768. op->mode = (mode & S_IALLUGO) | S_IFREG;
  769. else
  770. op->mode = 0;
  771. /* Must never be set by userspace */
  772. flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
  773. /*
  774. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  775. * check for O_DSYNC if the need any syncing at all we enforce it's
  776. * always set instead of having to deal with possibly weird behaviour
  777. * for malicious applications setting only __O_SYNC.
  778. */
  779. if (flags & __O_SYNC)
  780. flags |= O_DSYNC;
  781. if (flags & __O_TMPFILE) {
  782. if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
  783. return -EINVAL;
  784. acc_mode = MAY_OPEN | ACC_MODE(flags);
  785. if (!(acc_mode & MAY_WRITE))
  786. return -EINVAL;
  787. } else if (flags & O_PATH) {
  788. /*
  789. * If we have O_PATH in the open flag. Then we
  790. * cannot have anything other than the below set of flags
  791. */
  792. flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
  793. acc_mode = 0;
  794. } else {
  795. acc_mode = MAY_OPEN | ACC_MODE(flags);
  796. }
  797. op->open_flag = flags;
  798. /* O_TRUNC implies we need access checks for write permissions */
  799. if (flags & O_TRUNC)
  800. acc_mode |= MAY_WRITE;
  801. /* Allow the LSM permission hook to distinguish append
  802. access from general write access. */
  803. if (flags & O_APPEND)
  804. acc_mode |= MAY_APPEND;
  805. op->acc_mode = acc_mode;
  806. op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
  807. if (flags & O_CREAT) {
  808. op->intent |= LOOKUP_CREATE;
  809. if (flags & O_EXCL)
  810. op->intent |= LOOKUP_EXCL;
  811. }
  812. if (flags & O_DIRECTORY)
  813. lookup_flags |= LOOKUP_DIRECTORY;
  814. if (!(flags & O_NOFOLLOW))
  815. lookup_flags |= LOOKUP_FOLLOW;
  816. op->lookup_flags = lookup_flags;
  817. return 0;
  818. }
  819. /**
  820. * file_open_name - open file and return file pointer
  821. *
  822. * @name: struct filename containing path to open
  823. * @flags: open flags as per the open(2) second argument
  824. * @mode: mode for the new file if O_CREAT is set, else ignored
  825. *
  826. * This is the helper to open a file from kernelspace if you really
  827. * have to. But in generally you should not do this, so please move
  828. * along, nothing to see here..
  829. */
  830. struct file *file_open_name(struct filename *name, int flags, umode_t mode)
  831. {
  832. struct open_flags op;
  833. int err = build_open_flags(flags, mode, &op);
  834. return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
  835. }
  836. /**
  837. * filp_open - open file and return file pointer
  838. *
  839. * @filename: path to open
  840. * @flags: open flags as per the open(2) second argument
  841. * @mode: mode for the new file if O_CREAT is set, else ignored
  842. *
  843. * This is the helper to open a file from kernelspace if you really
  844. * have to. But in generally you should not do this, so please move
  845. * along, nothing to see here..
  846. */
  847. struct file *filp_open(const char *filename, int flags, umode_t mode)
  848. {
  849. struct filename *name = getname_kernel(filename);
  850. struct file *file = ERR_CAST(name);
  851. if (!IS_ERR(name)) {
  852. file = file_open_name(name, flags, mode);
  853. putname(name);
  854. }
  855. return file;
  856. }
  857. EXPORT_SYMBOL(filp_open);
  858. struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  859. const char *filename, int flags)
  860. {
  861. struct open_flags op;
  862. int err = build_open_flags(flags, 0, &op);
  863. if (err)
  864. return ERR_PTR(err);
  865. if (flags & O_CREAT)
  866. return ERR_PTR(-EINVAL);
  867. return do_file_open_root(dentry, mnt, filename, &op);
  868. }
  869. EXPORT_SYMBOL(file_open_root);
  870. long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
  871. {
  872. struct open_flags op;
  873. int fd = build_open_flags(flags, mode, &op);
  874. struct filename *tmp;
  875. if (fd)
  876. return fd;
  877. tmp = getname(filename);
  878. if (IS_ERR(tmp))
  879. return PTR_ERR(tmp);
  880. fd = get_unused_fd_flags(flags);
  881. if (fd >= 0) {
  882. struct file *f = do_filp_open(dfd, tmp, &op);
  883. if (IS_ERR(f)) {
  884. put_unused_fd(fd);
  885. fd = PTR_ERR(f);
  886. } else {
  887. fsnotify_open(f);
  888. fd_install(fd, f);
  889. }
  890. }
  891. putname(tmp);
  892. return fd;
  893. }
  894. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  895. {
  896. if (force_o_largefile())
  897. flags |= O_LARGEFILE;
  898. return do_sys_open(AT_FDCWD, filename, flags, mode);
  899. }
  900. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  901. umode_t, mode)
  902. {
  903. if (force_o_largefile())
  904. flags |= O_LARGEFILE;
  905. return do_sys_open(dfd, filename, flags, mode);
  906. }
  907. #ifndef __alpha__
  908. /*
  909. * For backward compatibility? Maybe this should be moved
  910. * into arch/i386 instead?
  911. */
  912. SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
  913. {
  914. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  915. }
  916. #endif
  917. /*
  918. * "id" is the POSIX thread ID. We use the
  919. * files pointer for this..
  920. */
  921. int filp_close(struct file *filp, fl_owner_t id)
  922. {
  923. int retval = 0;
  924. if (!file_count(filp)) {
  925. printk(KERN_ERR "VFS: Close: file count is 0\n");
  926. return 0;
  927. }
  928. if (filp->f_op->flush)
  929. retval = filp->f_op->flush(filp, id);
  930. if (likely(!(filp->f_mode & FMODE_PATH))) {
  931. dnotify_flush(filp, id);
  932. locks_remove_posix(filp, id);
  933. }
  934. fput(filp);
  935. return retval;
  936. }
  937. EXPORT_SYMBOL(filp_close);
  938. /*
  939. * Careful here! We test whether the file pointer is NULL before
  940. * releasing the fd. This ensures that one clone task can't release
  941. * an fd while another clone is opening it.
  942. */
  943. SYSCALL_DEFINE1(close, unsigned int, fd)
  944. {
  945. int retval = __close_fd(current->files, fd);
  946. /* can't restart close syscall because file table entry was cleared */
  947. if (unlikely(retval == -ERESTARTSYS ||
  948. retval == -ERESTARTNOINTR ||
  949. retval == -ERESTARTNOHAND ||
  950. retval == -ERESTART_RESTARTBLOCK))
  951. retval = -EINTR;
  952. return retval;
  953. }
  954. EXPORT_SYMBOL(sys_close);
  955. /*
  956. * This routine simulates a hangup on the tty, to arrange that users
  957. * are given clean terminals at login time.
  958. */
  959. SYSCALL_DEFINE0(vhangup)
  960. {
  961. if (capable(CAP_SYS_TTY_CONFIG)) {
  962. tty_vhangup_self();
  963. return 0;
  964. }
  965. return -EPERM;
  966. }
  967. /*
  968. * Called when an inode is about to be open.
  969. * We use this to disallow opening large files on 32bit systems if
  970. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  971. * on this flag in sys_open.
  972. */
  973. int generic_file_open(struct inode * inode, struct file * filp)
  974. {
  975. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  976. return -EOVERFLOW;
  977. return 0;
  978. }
  979. EXPORT_SYMBOL(generic_file_open);
  980. /*
  981. * This is used by subsystems that don't want seekable
  982. * file descriptors. The function is not supposed to ever fail, the only
  983. * reason it returns an 'int' and not 'void' is so that it can be plugged
  984. * directly into file_operations structure.
  985. */
  986. int nonseekable_open(struct inode *inode, struct file *filp)
  987. {
  988. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  989. return 0;
  990. }
  991. EXPORT_SYMBOL(nonseekable_open);