open.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. old_cred = override_creds(override_cred);
  313. retry:
  314. res = user_path_at(dfd, filename, lookup_flags, &path);
  315. if (res)
  316. goto out;
  317. inode = d_backing_inode(path.dentry);
  318. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  319. /*
  320. * MAY_EXEC on regular files is denied if the fs is mounted
  321. * with the "noexec" flag.
  322. */
  323. res = -EACCES;
  324. if (path_noexec(&path))
  325. goto out_path_release;
  326. }
  327. res = inode_permission(inode, mode | MAY_ACCESS);
  328. /* SuS v2 requires we report a read only fs too */
  329. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  330. goto out_path_release;
  331. /*
  332. * This is a rare case where using __mnt_is_readonly()
  333. * is OK without a mnt_want/drop_write() pair. Since
  334. * no actual write to the fs is performed here, we do
  335. * not need to telegraph to that to anyone.
  336. *
  337. * By doing this, we accept that this access is
  338. * inherently racy and know that the fs may change
  339. * state before we even see this result.
  340. */
  341. if (__mnt_is_readonly(path.mnt))
  342. res = -EROFS;
  343. out_path_release:
  344. path_put(&path);
  345. if (retry_estale(res, lookup_flags)) {
  346. lookup_flags |= LOOKUP_REVAL;
  347. goto retry;
  348. }
  349. out:
  350. revert_creds(old_cred);
  351. put_cred(override_cred);
  352. return res;
  353. }
  354. SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
  355. {
  356. return do_faccessat(dfd, filename, mode);
  357. }
  358. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  359. {
  360. return do_faccessat(AT_FDCWD, filename, mode);
  361. }
  362. int ksys_chdir(const char __user *filename)
  363. {
  364. struct path path;
  365. int error;
  366. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  367. retry:
  368. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  369. if (error)
  370. goto out;
  371. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  372. if (error)
  373. goto dput_and_out;
  374. set_fs_pwd(current->fs, &path);
  375. dput_and_out:
  376. path_put(&path);
  377. if (retry_estale(error, lookup_flags)) {
  378. lookup_flags |= LOOKUP_REVAL;
  379. goto retry;
  380. }
  381. out:
  382. return error;
  383. }
  384. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  385. {
  386. return ksys_chdir(filename);
  387. }
  388. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  389. {
  390. struct fd f = fdget_raw(fd);
  391. int error;
  392. error = -EBADF;
  393. if (!f.file)
  394. goto out;
  395. error = -ENOTDIR;
  396. if (!d_can_lookup(f.file->f_path.dentry))
  397. goto out_putf;
  398. error = inode_permission(file_inode(f.file), MAY_EXEC | MAY_CHDIR);
  399. if (!error)
  400. set_fs_pwd(current->fs, &f.file->f_path);
  401. out_putf:
  402. fdput(f);
  403. out:
  404. return error;
  405. }
  406. int ksys_chroot(const char __user *filename)
  407. {
  408. struct path path;
  409. int error;
  410. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  411. retry:
  412. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  413. if (error)
  414. goto out;
  415. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  416. if (error)
  417. goto dput_and_out;
  418. error = -EPERM;
  419. if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
  420. goto dput_and_out;
  421. error = security_path_chroot(&path);
  422. if (error)
  423. goto dput_and_out;
  424. set_fs_root(current->fs, &path);
  425. error = 0;
  426. dput_and_out:
  427. path_put(&path);
  428. if (retry_estale(error, lookup_flags)) {
  429. lookup_flags |= LOOKUP_REVAL;
  430. goto retry;
  431. }
  432. out:
  433. return error;
  434. }
  435. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  436. {
  437. return ksys_chroot(filename);
  438. }
  439. static int chmod_common(const struct path *path, umode_t mode)
  440. {
  441. struct inode *inode = path->dentry->d_inode;
  442. struct inode *delegated_inode = NULL;
  443. struct iattr newattrs;
  444. int error;
  445. error = mnt_want_write(path->mnt);
  446. if (error)
  447. return error;
  448. retry_deleg:
  449. inode_lock(inode);
  450. error = security_path_chmod(path, mode);
  451. if (error)
  452. goto out_unlock;
  453. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  454. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  455. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  456. out_unlock:
  457. inode_unlock(inode);
  458. if (delegated_inode) {
  459. error = break_deleg_wait(&delegated_inode);
  460. if (!error)
  461. goto retry_deleg;
  462. }
  463. mnt_drop_write(path->mnt);
  464. return error;
  465. }
  466. int ksys_fchmod(unsigned int fd, umode_t mode)
  467. {
  468. struct fd f = fdget(fd);
  469. int err = -EBADF;
  470. if (f.file) {
  471. audit_file(f.file);
  472. err = chmod_common(&f.file->f_path, mode);
  473. fdput(f);
  474. }
  475. return err;
  476. }
  477. SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
  478. {
  479. return ksys_fchmod(fd, mode);
  480. }
  481. int do_fchmodat(int dfd, const char __user *filename, umode_t mode)
  482. {
  483. struct path path;
  484. int error;
  485. unsigned int lookup_flags = LOOKUP_FOLLOW;
  486. retry:
  487. error = user_path_at(dfd, filename, lookup_flags, &path);
  488. if (!error) {
  489. error = chmod_common(&path, mode);
  490. path_put(&path);
  491. if (retry_estale(error, lookup_flags)) {
  492. lookup_flags |= LOOKUP_REVAL;
  493. goto retry;
  494. }
  495. }
  496. return error;
  497. }
  498. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename,
  499. umode_t, mode)
  500. {
  501. return do_fchmodat(dfd, filename, mode);
  502. }
  503. SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
  504. {
  505. return do_fchmodat(AT_FDCWD, filename, mode);
  506. }
  507. static int chown_common(const struct path *path, uid_t user, gid_t group)
  508. {
  509. struct inode *inode = path->dentry->d_inode;
  510. struct inode *delegated_inode = NULL;
  511. int error;
  512. struct iattr newattrs;
  513. kuid_t uid;
  514. kgid_t gid;
  515. uid = make_kuid(current_user_ns(), user);
  516. gid = make_kgid(current_user_ns(), group);
  517. retry_deleg:
  518. newattrs.ia_valid = ATTR_CTIME;
  519. if (user != (uid_t) -1) {
  520. if (!uid_valid(uid))
  521. return -EINVAL;
  522. newattrs.ia_valid |= ATTR_UID;
  523. newattrs.ia_uid = uid;
  524. }
  525. if (group != (gid_t) -1) {
  526. if (!gid_valid(gid))
  527. return -EINVAL;
  528. newattrs.ia_valid |= ATTR_GID;
  529. newattrs.ia_gid = gid;
  530. }
  531. if (!S_ISDIR(inode->i_mode))
  532. newattrs.ia_valid |=
  533. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  534. inode_lock(inode);
  535. error = security_path_chown(path, uid, gid);
  536. if (!error)
  537. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  538. inode_unlock(inode);
  539. if (delegated_inode) {
  540. error = break_deleg_wait(&delegated_inode);
  541. if (!error)
  542. goto retry_deleg;
  543. }
  544. return error;
  545. }
  546. int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
  547. int flag)
  548. {
  549. struct path path;
  550. int error = -EINVAL;
  551. int lookup_flags;
  552. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  553. goto out;
  554. lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  555. if (flag & AT_EMPTY_PATH)
  556. lookup_flags |= LOOKUP_EMPTY;
  557. retry:
  558. error = user_path_at(dfd, filename, lookup_flags, &path);
  559. if (error)
  560. goto out;
  561. error = mnt_want_write(path.mnt);
  562. if (error)
  563. goto out_release;
  564. error = chown_common(&path, user, group);
  565. mnt_drop_write(path.mnt);
  566. out_release:
  567. path_put(&path);
  568. if (retry_estale(error, lookup_flags)) {
  569. lookup_flags |= LOOKUP_REVAL;
  570. goto retry;
  571. }
  572. out:
  573. return error;
  574. }
  575. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  576. gid_t, group, int, flag)
  577. {
  578. return do_fchownat(dfd, filename, user, group, flag);
  579. }
  580. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  581. {
  582. return do_fchownat(AT_FDCWD, filename, user, group, 0);
  583. }
  584. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  585. {
  586. return do_fchownat(AT_FDCWD, filename, user, group,
  587. AT_SYMLINK_NOFOLLOW);
  588. }
  589. int ksys_fchown(unsigned int fd, uid_t user, gid_t group)
  590. {
  591. struct fd f = fdget(fd);
  592. int error = -EBADF;
  593. if (!f.file)
  594. goto out;
  595. error = mnt_want_write_file(f.file);
  596. if (error)
  597. goto out_fput;
  598. audit_file(f.file);
  599. error = chown_common(&f.file->f_path, user, group);
  600. mnt_drop_write_file(f.file);
  601. out_fput:
  602. fdput(f);
  603. out:
  604. return error;
  605. }
  606. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  607. {
  608. return ksys_fchown(fd, user, group);
  609. }
  610. static int do_dentry_open(struct file *f,
  611. struct inode *inode,
  612. int (*open)(struct inode *, struct file *))
  613. {
  614. static const struct file_operations empty_fops = {};
  615. int error;
  616. path_get(&f->f_path);
  617. f->f_inode = inode;
  618. f->f_mapping = inode->i_mapping;
  619. /* Ensure that we skip any errors that predate opening of the file */
  620. f->f_wb_err = filemap_sample_wb_err(f->f_mapping);
  621. if (unlikely(f->f_flags & O_PATH)) {
  622. f->f_mode = FMODE_PATH | FMODE_OPENED;
  623. f->f_op = &empty_fops;
  624. return 0;
  625. }
  626. if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
  627. error = get_write_access(inode);
  628. if (unlikely(error))
  629. goto cleanup_file;
  630. error = __mnt_want_write(f->f_path.mnt);
  631. if (unlikely(error)) {
  632. put_write_access(inode);
  633. goto cleanup_file;
  634. }
  635. f->f_mode |= FMODE_WRITER;
  636. }
  637. /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
  638. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
  639. f->f_mode |= FMODE_ATOMIC_POS;
  640. f->f_op = fops_get(inode->i_fop);
  641. if (unlikely(WARN_ON(!f->f_op))) {
  642. error = -ENODEV;
  643. goto cleanup_all;
  644. }
  645. error = security_file_open(f);
  646. if (error)
  647. goto cleanup_all;
  648. error = break_lease(locks_inode(f), f->f_flags);
  649. if (error)
  650. goto cleanup_all;
  651. /* normally all 3 are set; ->open() can clear them if needed */
  652. f->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
  653. if (!open)
  654. open = f->f_op->open;
  655. if (open) {
  656. error = open(inode, f);
  657. if (error)
  658. goto cleanup_all;
  659. }
  660. f->f_mode |= FMODE_OPENED;
  661. if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  662. i_readcount_inc(inode);
  663. if ((f->f_mode & FMODE_READ) &&
  664. likely(f->f_op->read || f->f_op->read_iter))
  665. f->f_mode |= FMODE_CAN_READ;
  666. if ((f->f_mode & FMODE_WRITE) &&
  667. likely(f->f_op->write || f->f_op->write_iter))
  668. f->f_mode |= FMODE_CAN_WRITE;
  669. f->f_write_hint = WRITE_LIFE_NOT_SET;
  670. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  671. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  672. /* NB: we're sure to have correct a_ops only after f_op->open */
  673. if (f->f_flags & O_DIRECT) {
  674. if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
  675. return -EINVAL;
  676. }
  677. return 0;
  678. cleanup_all:
  679. if (WARN_ON_ONCE(error > 0))
  680. error = -EINVAL;
  681. fops_put(f->f_op);
  682. if (f->f_mode & FMODE_WRITER) {
  683. put_write_access(inode);
  684. __mnt_drop_write(f->f_path.mnt);
  685. }
  686. cleanup_file:
  687. path_put(&f->f_path);
  688. f->f_path.mnt = NULL;
  689. f->f_path.dentry = NULL;
  690. f->f_inode = NULL;
  691. return error;
  692. }
  693. /**
  694. * finish_open - finish opening a file
  695. * @file: file pointer
  696. * @dentry: pointer to dentry
  697. * @open: open callback
  698. * @opened: state of open
  699. *
  700. * This can be used to finish opening a file passed to i_op->atomic_open().
  701. *
  702. * If the open callback is set to NULL, then the standard f_op->open()
  703. * filesystem callback is substituted.
  704. *
  705. * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
  706. * the return value of d_splice_alias(), then the caller needs to perform dput()
  707. * on it after finish_open().
  708. *
  709. * On successful return @file is a fully instantiated open file. After this, if
  710. * an error occurs in ->atomic_open(), it needs to clean up with fput().
  711. *
  712. * Returns zero on success or -errno if the open failed.
  713. */
  714. int finish_open(struct file *file, struct dentry *dentry,
  715. int (*open)(struct inode *, struct file *))
  716. {
  717. BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
  718. file->f_path.dentry = dentry;
  719. return do_dentry_open(file, d_backing_inode(dentry), open);
  720. }
  721. EXPORT_SYMBOL(finish_open);
  722. /**
  723. * finish_no_open - finish ->atomic_open() without opening the file
  724. *
  725. * @file: file pointer
  726. * @dentry: dentry or NULL (as returned from ->lookup())
  727. *
  728. * This can be used to set the result of a successful lookup in ->atomic_open().
  729. *
  730. * NB: unlike finish_open() this function does consume the dentry reference and
  731. * the caller need not dput() it.
  732. *
  733. * Returns "0" which must be the return value of ->atomic_open() after having
  734. * called this function.
  735. */
  736. int finish_no_open(struct file *file, struct dentry *dentry)
  737. {
  738. file->f_path.dentry = dentry;
  739. return 0;
  740. }
  741. EXPORT_SYMBOL(finish_no_open);
  742. char *file_path(struct file *filp, char *buf, int buflen)
  743. {
  744. return d_path(&filp->f_path, buf, buflen);
  745. }
  746. EXPORT_SYMBOL(file_path);
  747. /**
  748. * vfs_open - open the file at the given path
  749. * @path: path to open
  750. * @file: newly allocated file with f_flag initialized
  751. * @cred: credentials to use
  752. */
  753. int vfs_open(const struct path *path, struct file *file)
  754. {
  755. file->f_path = *path;
  756. return do_dentry_open(file, d_backing_inode(path->dentry), NULL);
  757. }
  758. struct file *dentry_open(const struct path *path, int flags,
  759. const struct cred *cred)
  760. {
  761. int error;
  762. struct file *f;
  763. validate_creds(cred);
  764. /* We must always pass in a valid mount pointer. */
  765. BUG_ON(!path->mnt);
  766. f = alloc_empty_file(flags, cred);
  767. if (!IS_ERR(f)) {
  768. error = vfs_open(path, f);
  769. if (error) {
  770. fput(f);
  771. f = ERR_PTR(error);
  772. }
  773. }
  774. return f;
  775. }
  776. EXPORT_SYMBOL(dentry_open);
  777. struct file *open_with_fake_path(const struct path *path, int flags,
  778. struct inode *inode, const struct cred *cred)
  779. {
  780. struct file *f = alloc_empty_file_noaccount(flags, cred);
  781. if (!IS_ERR(f)) {
  782. int error;
  783. f->f_path = *path;
  784. error = do_dentry_open(f, inode, NULL);
  785. if (error) {
  786. fput(f);
  787. f = ERR_PTR(error);
  788. }
  789. }
  790. return f;
  791. }
  792. EXPORT_SYMBOL(open_with_fake_path);
  793. static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
  794. {
  795. int lookup_flags = 0;
  796. int acc_mode = ACC_MODE(flags);
  797. /*
  798. * Clear out all open flags we don't know about so that we don't report
  799. * them in fcntl(F_GETFD) or similar interfaces.
  800. */
  801. flags &= VALID_OPEN_FLAGS;
  802. if (flags & (O_CREAT | __O_TMPFILE))
  803. op->mode = (mode & S_IALLUGO) | S_IFREG;
  804. else
  805. op->mode = 0;
  806. /* Must never be set by userspace */
  807. flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
  808. /*
  809. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  810. * check for O_DSYNC if the need any syncing at all we enforce it's
  811. * always set instead of having to deal with possibly weird behaviour
  812. * for malicious applications setting only __O_SYNC.
  813. */
  814. if (flags & __O_SYNC)
  815. flags |= O_DSYNC;
  816. if (flags & __O_TMPFILE) {
  817. if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
  818. return -EINVAL;
  819. if (!(acc_mode & MAY_WRITE))
  820. return -EINVAL;
  821. } else if (flags & O_PATH) {
  822. /*
  823. * If we have O_PATH in the open flag. Then we
  824. * cannot have anything other than the below set of flags
  825. */
  826. flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
  827. acc_mode = 0;
  828. }
  829. op->open_flag = flags;
  830. /* O_TRUNC implies we need access checks for write permissions */
  831. if (flags & O_TRUNC)
  832. acc_mode |= MAY_WRITE;
  833. /* Allow the LSM permission hook to distinguish append
  834. access from general write access. */
  835. if (flags & O_APPEND)
  836. acc_mode |= MAY_APPEND;
  837. op->acc_mode = acc_mode;
  838. op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
  839. if (flags & O_CREAT) {
  840. op->intent |= LOOKUP_CREATE;
  841. if (flags & O_EXCL)
  842. op->intent |= LOOKUP_EXCL;
  843. }
  844. if (flags & O_DIRECTORY)
  845. lookup_flags |= LOOKUP_DIRECTORY;
  846. if (!(flags & O_NOFOLLOW))
  847. lookup_flags |= LOOKUP_FOLLOW;
  848. op->lookup_flags = lookup_flags;
  849. return 0;
  850. }
  851. /**
  852. * file_open_name - open file and return file pointer
  853. *
  854. * @name: struct filename containing path to open
  855. * @flags: open flags as per the open(2) second argument
  856. * @mode: mode for the new file if O_CREAT is set, else ignored
  857. *
  858. * This is the helper to open a file from kernelspace if you really
  859. * have to. But in generally you should not do this, so please move
  860. * along, nothing to see here..
  861. */
  862. struct file *file_open_name(struct filename *name, int flags, umode_t mode)
  863. {
  864. struct open_flags op;
  865. int err = build_open_flags(flags, mode, &op);
  866. return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
  867. }
  868. /**
  869. * filp_open - open file and return file pointer
  870. *
  871. * @filename: path to open
  872. * @flags: open flags as per the open(2) second argument
  873. * @mode: mode for the new file if O_CREAT is set, else ignored
  874. *
  875. * This is the helper to open a file from kernelspace if you really
  876. * have to. But in generally you should not do this, so please move
  877. * along, nothing to see here..
  878. */
  879. struct file *filp_open(const char *filename, int flags, umode_t mode)
  880. {
  881. struct filename *name = getname_kernel(filename);
  882. struct file *file = ERR_CAST(name);
  883. if (!IS_ERR(name)) {
  884. file = file_open_name(name, flags, mode);
  885. putname(name);
  886. }
  887. return file;
  888. }
  889. EXPORT_SYMBOL(filp_open);
  890. struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  891. const char *filename, int flags, umode_t mode)
  892. {
  893. struct open_flags op;
  894. int err = build_open_flags(flags, mode, &op);
  895. if (err)
  896. return ERR_PTR(err);
  897. return do_file_open_root(dentry, mnt, filename, &op);
  898. }
  899. EXPORT_SYMBOL(file_open_root);
  900. long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
  901. {
  902. struct open_flags op;
  903. int fd = build_open_flags(flags, mode, &op);
  904. struct filename *tmp;
  905. if (fd)
  906. return fd;
  907. tmp = getname(filename);
  908. if (IS_ERR(tmp))
  909. return PTR_ERR(tmp);
  910. fd = get_unused_fd_flags(flags);
  911. if (fd >= 0) {
  912. struct file *f = do_filp_open(dfd, tmp, &op);
  913. if (IS_ERR(f)) {
  914. put_unused_fd(fd);
  915. fd = PTR_ERR(f);
  916. } else {
  917. fsnotify_open(f);
  918. fd_install(fd, f);
  919. }
  920. }
  921. putname(tmp);
  922. return fd;
  923. }
  924. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  925. {
  926. if (force_o_largefile())
  927. flags |= O_LARGEFILE;
  928. return do_sys_open(AT_FDCWD, filename, flags, mode);
  929. }
  930. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  931. umode_t, mode)
  932. {
  933. if (force_o_largefile())
  934. flags |= O_LARGEFILE;
  935. return do_sys_open(dfd, filename, flags, mode);
  936. }
  937. #ifdef CONFIG_COMPAT
  938. /*
  939. * Exactly like sys_open(), except that it doesn't set the
  940. * O_LARGEFILE flag.
  941. */
  942. COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  943. {
  944. return do_sys_open(AT_FDCWD, filename, flags, mode);
  945. }
  946. /*
  947. * Exactly like sys_openat(), except that it doesn't set the
  948. * O_LARGEFILE flag.
  949. */
  950. COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
  951. {
  952. return do_sys_open(dfd, filename, flags, mode);
  953. }
  954. #endif
  955. #ifndef __alpha__
  956. /*
  957. * For backward compatibility? Maybe this should be moved
  958. * into arch/i386 instead?
  959. */
  960. SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
  961. {
  962. return ksys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  963. }
  964. #endif
  965. /*
  966. * "id" is the POSIX thread ID. We use the
  967. * files pointer for this..
  968. */
  969. int filp_close(struct file *filp, fl_owner_t id)
  970. {
  971. int retval = 0;
  972. if (!file_count(filp)) {
  973. printk(KERN_ERR "VFS: Close: file count is 0\n");
  974. return 0;
  975. }
  976. if (filp->f_op->flush)
  977. retval = filp->f_op->flush(filp, id);
  978. if (likely(!(filp->f_mode & FMODE_PATH))) {
  979. dnotify_flush(filp, id);
  980. locks_remove_posix(filp, id);
  981. }
  982. fput(filp);
  983. return retval;
  984. }
  985. EXPORT_SYMBOL(filp_close);
  986. /*
  987. * Careful here! We test whether the file pointer is NULL before
  988. * releasing the fd. This ensures that one clone task can't release
  989. * an fd while another clone is opening it.
  990. */
  991. SYSCALL_DEFINE1(close, unsigned int, fd)
  992. {
  993. int retval = __close_fd(current->files, fd);
  994. /* can't restart close syscall because file table entry was cleared */
  995. if (unlikely(retval == -ERESTARTSYS ||
  996. retval == -ERESTARTNOINTR ||
  997. retval == -ERESTARTNOHAND ||
  998. retval == -ERESTART_RESTARTBLOCK))
  999. retval = -EINTR;
  1000. return retval;
  1001. }
  1002. /*
  1003. * This routine simulates a hangup on the tty, to arrange that users
  1004. * are given clean terminals at login time.
  1005. */
  1006. SYSCALL_DEFINE0(vhangup)
  1007. {
  1008. if (capable(CAP_SYS_TTY_CONFIG)) {
  1009. tty_vhangup_self();
  1010. return 0;
  1011. }
  1012. return -EPERM;
  1013. }
  1014. /*
  1015. * Called when an inode is about to be open.
  1016. * We use this to disallow opening large files on 32bit systems if
  1017. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  1018. * on this flag in sys_open.
  1019. */
  1020. int generic_file_open(struct inode * inode, struct file * filp)
  1021. {
  1022. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  1023. return -EOVERFLOW;
  1024. return 0;
  1025. }
  1026. EXPORT_SYMBOL(generic_file_open);
  1027. /*
  1028. * This is used by subsystems that don't want seekable
  1029. * file descriptors. The function is not supposed to ever fail, the only
  1030. * reason it returns an 'int' and not 'void' is so that it can be plugged
  1031. * directly into file_operations structure.
  1032. */
  1033. int nonseekable_open(struct inode *inode, struct file *filp)
  1034. {
  1035. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1036. return 0;
  1037. }
  1038. EXPORT_SYMBOL(nonseekable_open);