open.c 30 KB

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