open.c 29 KB

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