open.c 30 KB

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