open.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/open.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. #include <linux/string.h>
  8. #include <linux/mm.h>
  9. #include <linux/file.h>
  10. #include <linux/fdtable.h>
  11. #include <linux/fsnotify.h>
  12. #include <linux/module.h>
  13. #include <linux/tty.h>
  14. #include <linux/namei.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/capability.h>
  17. #include <linux/securebits.h>
  18. #include <linux/security.h>
  19. #include <linux/mount.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/slab.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/fs.h>
  24. #include <linux/personality.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/rcupdate.h>
  28. #include <linux/audit.h>
  29. #include <linux/falloc.h>
  30. #include <linux/fs_struct.h>
  31. #include <linux/dnotify.h>
  32. #include <linux/compat.h>
  33. #include <linux/mnt_idmapping.h>
  34. #include <linux/filelock.h>
  35. #include "internal.h"
  36. int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
  37. loff_t length, unsigned int time_attrs, struct file *filp)
  38. {
  39. int ret;
  40. struct iattr newattrs;
  41. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  42. if (length < 0)
  43. return -EINVAL;
  44. newattrs.ia_size = length;
  45. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  46. if (filp) {
  47. newattrs.ia_file = filp;
  48. newattrs.ia_valid |= ATTR_FILE;
  49. }
  50. /* Remove suid, sgid, and file capabilities on truncate too */
  51. ret = dentry_needs_remove_privs(idmap, dentry);
  52. if (ret < 0)
  53. return ret;
  54. if (ret)
  55. newattrs.ia_valid |= ret | ATTR_FORCE;
  56. inode_lock(dentry->d_inode);
  57. /* Note any delegations or leases have already been broken: */
  58. ret = notify_change(idmap, dentry, &newattrs, NULL);
  59. inode_unlock(dentry->d_inode);
  60. return ret;
  61. }
  62. long vfs_truncate(const struct path *path, loff_t length)
  63. {
  64. struct mnt_idmap *idmap;
  65. struct inode *inode;
  66. long error;
  67. inode = path->dentry->d_inode;
  68. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  69. if (S_ISDIR(inode->i_mode))
  70. return -EISDIR;
  71. if (!S_ISREG(inode->i_mode))
  72. return -EINVAL;
  73. error = mnt_want_write(path->mnt);
  74. if (error)
  75. goto out;
  76. idmap = mnt_idmap(path->mnt);
  77. error = inode_permission(idmap, inode, MAY_WRITE);
  78. if (error)
  79. goto mnt_drop_write_and_out;
  80. error = -EPERM;
  81. if (IS_APPEND(inode))
  82. goto mnt_drop_write_and_out;
  83. error = get_write_access(inode);
  84. if (error)
  85. goto mnt_drop_write_and_out;
  86. /*
  87. * Make sure that there are no leases. get_write_access() protects
  88. * against the truncate racing with a lease-granting setlease().
  89. */
  90. error = break_lease(inode, O_WRONLY);
  91. if (error)
  92. goto put_write_and_out;
  93. error = security_path_truncate(path);
  94. if (!error)
  95. error = do_truncate(idmap, path->dentry, length, 0, NULL);
  96. put_write_and_out:
  97. put_write_access(inode);
  98. mnt_drop_write_and_out:
  99. mnt_drop_write(path->mnt);
  100. out:
  101. return error;
  102. }
  103. EXPORT_SYMBOL_GPL(vfs_truncate);
  104. long do_sys_truncate(const char __user *pathname, loff_t length)
  105. {
  106. unsigned int lookup_flags = LOOKUP_FOLLOW;
  107. struct path path;
  108. int error;
  109. if (length < 0) /* sorry, but loff_t says... */
  110. return -EINVAL;
  111. retry:
  112. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  113. if (!error) {
  114. error = vfs_truncate(&path, length);
  115. path_put(&path);
  116. }
  117. if (retry_estale(error, lookup_flags)) {
  118. lookup_flags |= LOOKUP_REVAL;
  119. goto retry;
  120. }
  121. return error;
  122. }
  123. SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
  124. {
  125. return do_sys_truncate(path, length);
  126. }
  127. #ifdef CONFIG_COMPAT
  128. COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
  129. {
  130. return do_sys_truncate(path, length);
  131. }
  132. #endif
  133. long do_ftruncate(struct file *file, loff_t length, int small)
  134. {
  135. struct inode *inode;
  136. struct dentry *dentry;
  137. int error;
  138. /* explicitly opened as large or we are on 64-bit box */
  139. if (file->f_flags & O_LARGEFILE)
  140. small = 0;
  141. dentry = file->f_path.dentry;
  142. inode = dentry->d_inode;
  143. if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
  144. return -EINVAL;
  145. /* Cannot ftruncate over 2^31 bytes without large file support */
  146. if (small && length > MAX_NON_LFS)
  147. return -EINVAL;
  148. /* Check IS_APPEND on real upper inode */
  149. if (IS_APPEND(file_inode(file)))
  150. return -EPERM;
  151. sb_start_write(inode->i_sb);
  152. error = security_file_truncate(file);
  153. if (!error)
  154. error = do_truncate(file_mnt_idmap(file), dentry, length,
  155. ATTR_MTIME | ATTR_CTIME, file);
  156. sb_end_write(inode->i_sb);
  157. return error;
  158. }
  159. long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  160. {
  161. struct fd f;
  162. int error;
  163. if (length < 0)
  164. return -EINVAL;
  165. f = fdget(fd);
  166. if (!fd_file(f))
  167. return -EBADF;
  168. error = do_ftruncate(fd_file(f), length, small);
  169. fdput(f);
  170. return error;
  171. }
  172. SYSCALL_DEFINE2(ftruncate, unsigned int, fd, off_t, length)
  173. {
  174. return do_sys_ftruncate(fd, length, 1);
  175. }
  176. #ifdef CONFIG_COMPAT
  177. COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_off_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. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_TRUNCATE64)
  194. COMPAT_SYSCALL_DEFINE3(truncate64, const char __user *, pathname,
  195. compat_arg_u64_dual(length))
  196. {
  197. return ksys_truncate(pathname, compat_arg_u64_glue(length));
  198. }
  199. #endif
  200. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FTRUNCATE64)
  201. COMPAT_SYSCALL_DEFINE3(ftruncate64, unsigned int, fd,
  202. compat_arg_u64_dual(length))
  203. {
  204. return ksys_ftruncate(fd, compat_arg_u64_glue(length));
  205. }
  206. #endif
  207. int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  208. {
  209. struct inode *inode = file_inode(file);
  210. long ret;
  211. loff_t sum;
  212. if (offset < 0 || len <= 0)
  213. return -EINVAL;
  214. if (mode & ~(FALLOC_FL_MODE_MASK | FALLOC_FL_KEEP_SIZE))
  215. return -EOPNOTSUPP;
  216. /*
  217. * Modes are exclusive, even if that is not obvious from the encoding
  218. * as bit masks and the mix with the flag in the same namespace.
  219. *
  220. * To make things even more complicated, FALLOC_FL_ALLOCATE_RANGE is
  221. * encoded as no bit set.
  222. */
  223. switch (mode & FALLOC_FL_MODE_MASK) {
  224. case FALLOC_FL_ALLOCATE_RANGE:
  225. case FALLOC_FL_UNSHARE_RANGE:
  226. case FALLOC_FL_ZERO_RANGE:
  227. break;
  228. case FALLOC_FL_PUNCH_HOLE:
  229. if (!(mode & FALLOC_FL_KEEP_SIZE))
  230. return -EOPNOTSUPP;
  231. break;
  232. case FALLOC_FL_COLLAPSE_RANGE:
  233. case FALLOC_FL_INSERT_RANGE:
  234. if (mode & FALLOC_FL_KEEP_SIZE)
  235. return -EOPNOTSUPP;
  236. break;
  237. default:
  238. return -EOPNOTSUPP;
  239. }
  240. if (!(file->f_mode & FMODE_WRITE))
  241. return -EBADF;
  242. /*
  243. * On append-only files only space preallocation is supported.
  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. ret = fsnotify_file_area_perm(file, MAY_WRITE, &offset, len);
  262. if (ret)
  263. return ret;
  264. if (S_ISFIFO(inode->i_mode))
  265. return -ESPIPE;
  266. if (S_ISDIR(inode->i_mode))
  267. return -EISDIR;
  268. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  269. return -ENODEV;
  270. /* Check for wraparound */
  271. if (check_add_overflow(offset, len, &sum))
  272. return -EFBIG;
  273. if (sum > inode->i_sb->s_maxbytes)
  274. return -EFBIG;
  275. if (!file->f_op->fallocate)
  276. return -EOPNOTSUPP;
  277. file_start_write(file);
  278. ret = file->f_op->fallocate(file, mode, offset, len);
  279. /*
  280. * Create inotify and fanotify events.
  281. *
  282. * To keep the logic simple always create events if fallocate succeeds.
  283. * This implies that events are even created if the file size remains
  284. * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
  285. */
  286. if (ret == 0)
  287. fsnotify_modify(file);
  288. file_end_write(file);
  289. return ret;
  290. }
  291. EXPORT_SYMBOL_GPL(vfs_fallocate);
  292. int ksys_fallocate(int fd, int mode, loff_t offset, loff_t len)
  293. {
  294. struct fd f = fdget(fd);
  295. int error = -EBADF;
  296. if (fd_file(f)) {
  297. error = vfs_fallocate(fd_file(f), mode, offset, len);
  298. fdput(f);
  299. }
  300. return error;
  301. }
  302. SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
  303. {
  304. return ksys_fallocate(fd, mode, offset, len);
  305. }
  306. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FALLOCATE)
  307. COMPAT_SYSCALL_DEFINE6(fallocate, int, fd, int, mode, compat_arg_u64_dual(offset),
  308. compat_arg_u64_dual(len))
  309. {
  310. return ksys_fallocate(fd, mode, compat_arg_u64_glue(offset),
  311. compat_arg_u64_glue(len));
  312. }
  313. #endif
  314. /*
  315. * access() needs to use the real uid/gid, not the effective uid/gid.
  316. * We do this by temporarily clearing all FS-related capabilities and
  317. * switching the fsuid/fsgid around to the real ones.
  318. *
  319. * Creating new credentials is expensive, so we try to skip doing it,
  320. * which we can if the result would match what we already got.
  321. */
  322. static bool access_need_override_creds(int flags)
  323. {
  324. const struct cred *cred;
  325. if (flags & AT_EACCESS)
  326. return false;
  327. cred = current_cred();
  328. if (!uid_eq(cred->fsuid, cred->uid) ||
  329. !gid_eq(cred->fsgid, cred->gid))
  330. return true;
  331. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  332. kuid_t root_uid = make_kuid(cred->user_ns, 0);
  333. if (!uid_eq(cred->uid, root_uid)) {
  334. if (!cap_isclear(cred->cap_effective))
  335. return true;
  336. } else {
  337. if (!cap_isidentical(cred->cap_effective,
  338. cred->cap_permitted))
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344. static const struct cred *access_override_creds(void)
  345. {
  346. const struct cred *old_cred;
  347. struct cred *override_cred;
  348. override_cred = prepare_creds();
  349. if (!override_cred)
  350. return NULL;
  351. /*
  352. * XXX access_need_override_creds performs checks in hopes of skipping
  353. * this work. Make sure it stays in sync if making any changes in this
  354. * routine.
  355. */
  356. override_cred->fsuid = override_cred->uid;
  357. override_cred->fsgid = override_cred->gid;
  358. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  359. /* Clear the capabilities if we switch to a non-root user */
  360. kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
  361. if (!uid_eq(override_cred->uid, root_uid))
  362. cap_clear(override_cred->cap_effective);
  363. else
  364. override_cred->cap_effective =
  365. override_cred->cap_permitted;
  366. }
  367. /*
  368. * The new set of credentials can *only* be used in
  369. * task-synchronous circumstances, and does not need
  370. * RCU freeing, unless somebody then takes a separate
  371. * reference to it.
  372. *
  373. * NOTE! This is _only_ true because this credential
  374. * is used purely for override_creds() that installs
  375. * it as the subjective cred. Other threads will be
  376. * accessing ->real_cred, not the subjective cred.
  377. *
  378. * If somebody _does_ make a copy of this (using the
  379. * 'get_current_cred()' function), that will clear the
  380. * non_rcu field, because now that other user may be
  381. * expecting RCU freeing. But normal thread-synchronous
  382. * cred accesses will keep things non-racy to avoid RCU
  383. * freeing.
  384. */
  385. override_cred->non_rcu = 1;
  386. old_cred = override_creds(override_cred);
  387. /* override_cred() gets its own ref */
  388. put_cred(override_cred);
  389. return old_cred;
  390. }
  391. static long do_faccessat(int dfd, const char __user *filename, int mode, int flags)
  392. {
  393. struct path path;
  394. struct inode *inode;
  395. int res;
  396. unsigned int lookup_flags = LOOKUP_FOLLOW;
  397. const struct cred *old_cred = NULL;
  398. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  399. return -EINVAL;
  400. if (flags & ~(AT_EACCESS | AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
  401. return -EINVAL;
  402. if (flags & AT_SYMLINK_NOFOLLOW)
  403. lookup_flags &= ~LOOKUP_FOLLOW;
  404. if (flags & AT_EMPTY_PATH)
  405. lookup_flags |= LOOKUP_EMPTY;
  406. if (access_need_override_creds(flags)) {
  407. old_cred = access_override_creds();
  408. if (!old_cred)
  409. return -ENOMEM;
  410. }
  411. retry:
  412. res = user_path_at(dfd, filename, lookup_flags, &path);
  413. if (res)
  414. goto out;
  415. inode = d_backing_inode(path.dentry);
  416. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  417. /*
  418. * MAY_EXEC on regular files is denied if the fs is mounted
  419. * with the "noexec" flag.
  420. */
  421. res = -EACCES;
  422. if (path_noexec(&path))
  423. goto out_path_release;
  424. }
  425. res = inode_permission(mnt_idmap(path.mnt), inode, mode | MAY_ACCESS);
  426. /* SuS v2 requires we report a read only fs too */
  427. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  428. goto out_path_release;
  429. /*
  430. * This is a rare case where using __mnt_is_readonly()
  431. * is OK without a mnt_want/drop_write() pair. Since
  432. * no actual write to the fs is performed here, we do
  433. * not need to telegraph to that to anyone.
  434. *
  435. * By doing this, we accept that this access is
  436. * inherently racy and know that the fs may change
  437. * state before we even see this result.
  438. */
  439. if (__mnt_is_readonly(path.mnt))
  440. res = -EROFS;
  441. out_path_release:
  442. path_put(&path);
  443. if (retry_estale(res, lookup_flags)) {
  444. lookup_flags |= LOOKUP_REVAL;
  445. goto retry;
  446. }
  447. out:
  448. if (old_cred)
  449. revert_creds(old_cred);
  450. return res;
  451. }
  452. SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
  453. {
  454. return do_faccessat(dfd, filename, mode, 0);
  455. }
  456. SYSCALL_DEFINE4(faccessat2, int, dfd, const char __user *, filename, int, mode,
  457. int, flags)
  458. {
  459. return do_faccessat(dfd, filename, mode, flags);
  460. }
  461. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  462. {
  463. return do_faccessat(AT_FDCWD, filename, mode, 0);
  464. }
  465. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  466. {
  467. struct path path;
  468. int error;
  469. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  470. retry:
  471. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  472. if (error)
  473. goto out;
  474. error = path_permission(&path, MAY_EXEC | MAY_CHDIR);
  475. if (error)
  476. goto dput_and_out;
  477. set_fs_pwd(current->fs, &path);
  478. dput_and_out:
  479. path_put(&path);
  480. if (retry_estale(error, lookup_flags)) {
  481. lookup_flags |= LOOKUP_REVAL;
  482. goto retry;
  483. }
  484. out:
  485. return error;
  486. }
  487. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  488. {
  489. struct fd f = fdget_raw(fd);
  490. int error;
  491. error = -EBADF;
  492. if (!fd_file(f))
  493. goto out;
  494. error = -ENOTDIR;
  495. if (!d_can_lookup(fd_file(f)->f_path.dentry))
  496. goto out_putf;
  497. error = file_permission(fd_file(f), MAY_EXEC | MAY_CHDIR);
  498. if (!error)
  499. set_fs_pwd(current->fs, &fd_file(f)->f_path);
  500. out_putf:
  501. fdput(f);
  502. out:
  503. return error;
  504. }
  505. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  506. {
  507. struct path path;
  508. int error;
  509. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  510. retry:
  511. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  512. if (error)
  513. goto out;
  514. error = path_permission(&path, MAY_EXEC | MAY_CHDIR);
  515. if (error)
  516. goto dput_and_out;
  517. error = -EPERM;
  518. if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
  519. goto dput_and_out;
  520. error = security_path_chroot(&path);
  521. if (error)
  522. goto dput_and_out;
  523. set_fs_root(current->fs, &path);
  524. error = 0;
  525. dput_and_out:
  526. path_put(&path);
  527. if (retry_estale(error, lookup_flags)) {
  528. lookup_flags |= LOOKUP_REVAL;
  529. goto retry;
  530. }
  531. out:
  532. return error;
  533. }
  534. int chmod_common(const struct path *path, umode_t mode)
  535. {
  536. struct inode *inode = path->dentry->d_inode;
  537. struct inode *delegated_inode = NULL;
  538. struct iattr newattrs;
  539. int error;
  540. error = mnt_want_write(path->mnt);
  541. if (error)
  542. return error;
  543. retry_deleg:
  544. inode_lock(inode);
  545. error = security_path_chmod(path, mode);
  546. if (error)
  547. goto out_unlock;
  548. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  549. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  550. error = notify_change(mnt_idmap(path->mnt), path->dentry,
  551. &newattrs, &delegated_inode);
  552. out_unlock:
  553. inode_unlock(inode);
  554. if (delegated_inode) {
  555. error = break_deleg_wait(&delegated_inode);
  556. if (!error)
  557. goto retry_deleg;
  558. }
  559. mnt_drop_write(path->mnt);
  560. return error;
  561. }
  562. int vfs_fchmod(struct file *file, umode_t mode)
  563. {
  564. audit_file(file);
  565. return chmod_common(&file->f_path, mode);
  566. }
  567. SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
  568. {
  569. struct fd f = fdget(fd);
  570. int err = -EBADF;
  571. if (fd_file(f)) {
  572. err = vfs_fchmod(fd_file(f), mode);
  573. fdput(f);
  574. }
  575. return err;
  576. }
  577. static int do_fchmodat(int dfd, const char __user *filename, umode_t mode,
  578. unsigned int flags)
  579. {
  580. struct path path;
  581. int error;
  582. unsigned int lookup_flags;
  583. if (unlikely(flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)))
  584. return -EINVAL;
  585. lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  586. if (flags & AT_EMPTY_PATH)
  587. lookup_flags |= LOOKUP_EMPTY;
  588. retry:
  589. error = user_path_at(dfd, filename, lookup_flags, &path);
  590. if (!error) {
  591. error = chmod_common(&path, mode);
  592. path_put(&path);
  593. if (retry_estale(error, lookup_flags)) {
  594. lookup_flags |= LOOKUP_REVAL;
  595. goto retry;
  596. }
  597. }
  598. return error;
  599. }
  600. SYSCALL_DEFINE4(fchmodat2, int, dfd, const char __user *, filename,
  601. umode_t, mode, unsigned int, flags)
  602. {
  603. return do_fchmodat(dfd, filename, mode, flags);
  604. }
  605. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename,
  606. umode_t, mode)
  607. {
  608. return do_fchmodat(dfd, filename, mode, 0);
  609. }
  610. SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
  611. {
  612. return do_fchmodat(AT_FDCWD, filename, mode, 0);
  613. }
  614. /*
  615. * Check whether @kuid is valid and if so generate and set vfsuid_t in
  616. * ia_vfsuid.
  617. *
  618. * Return: true if @kuid is valid, false if not.
  619. */
  620. static inline bool setattr_vfsuid(struct iattr *attr, kuid_t kuid)
  621. {
  622. if (!uid_valid(kuid))
  623. return false;
  624. attr->ia_valid |= ATTR_UID;
  625. attr->ia_vfsuid = VFSUIDT_INIT(kuid);
  626. return true;
  627. }
  628. /*
  629. * Check whether @kgid is valid and if so generate and set vfsgid_t in
  630. * ia_vfsgid.
  631. *
  632. * Return: true if @kgid is valid, false if not.
  633. */
  634. static inline bool setattr_vfsgid(struct iattr *attr, kgid_t kgid)
  635. {
  636. if (!gid_valid(kgid))
  637. return false;
  638. attr->ia_valid |= ATTR_GID;
  639. attr->ia_vfsgid = VFSGIDT_INIT(kgid);
  640. return true;
  641. }
  642. int chown_common(const struct path *path, uid_t user, gid_t group)
  643. {
  644. struct mnt_idmap *idmap;
  645. struct user_namespace *fs_userns;
  646. struct inode *inode = path->dentry->d_inode;
  647. struct inode *delegated_inode = NULL;
  648. int error;
  649. struct iattr newattrs;
  650. kuid_t uid;
  651. kgid_t gid;
  652. uid = make_kuid(current_user_ns(), user);
  653. gid = make_kgid(current_user_ns(), group);
  654. idmap = mnt_idmap(path->mnt);
  655. fs_userns = i_user_ns(inode);
  656. retry_deleg:
  657. newattrs.ia_vfsuid = INVALID_VFSUID;
  658. newattrs.ia_vfsgid = INVALID_VFSGID;
  659. newattrs.ia_valid = ATTR_CTIME;
  660. if ((user != (uid_t)-1) && !setattr_vfsuid(&newattrs, uid))
  661. return -EINVAL;
  662. if ((group != (gid_t)-1) && !setattr_vfsgid(&newattrs, gid))
  663. return -EINVAL;
  664. inode_lock(inode);
  665. if (!S_ISDIR(inode->i_mode))
  666. newattrs.ia_valid |= ATTR_KILL_SUID | ATTR_KILL_PRIV |
  667. setattr_should_drop_sgid(idmap, inode);
  668. /* Continue to send actual fs values, not the mount values. */
  669. error = security_path_chown(
  670. path,
  671. from_vfsuid(idmap, fs_userns, newattrs.ia_vfsuid),
  672. from_vfsgid(idmap, fs_userns, newattrs.ia_vfsgid));
  673. if (!error)
  674. error = notify_change(idmap, path->dentry, &newattrs,
  675. &delegated_inode);
  676. inode_unlock(inode);
  677. if (delegated_inode) {
  678. error = break_deleg_wait(&delegated_inode);
  679. if (!error)
  680. goto retry_deleg;
  681. }
  682. return error;
  683. }
  684. int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
  685. int flag)
  686. {
  687. struct path path;
  688. int error = -EINVAL;
  689. int lookup_flags;
  690. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  691. goto out;
  692. lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  693. if (flag & AT_EMPTY_PATH)
  694. lookup_flags |= LOOKUP_EMPTY;
  695. retry:
  696. error = user_path_at(dfd, filename, lookup_flags, &path);
  697. if (error)
  698. goto out;
  699. error = mnt_want_write(path.mnt);
  700. if (error)
  701. goto out_release;
  702. error = chown_common(&path, user, group);
  703. mnt_drop_write(path.mnt);
  704. out_release:
  705. path_put(&path);
  706. if (retry_estale(error, lookup_flags)) {
  707. lookup_flags |= LOOKUP_REVAL;
  708. goto retry;
  709. }
  710. out:
  711. return error;
  712. }
  713. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  714. gid_t, group, int, flag)
  715. {
  716. return do_fchownat(dfd, filename, user, group, flag);
  717. }
  718. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  719. {
  720. return do_fchownat(AT_FDCWD, filename, user, group, 0);
  721. }
  722. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  723. {
  724. return do_fchownat(AT_FDCWD, filename, user, group,
  725. AT_SYMLINK_NOFOLLOW);
  726. }
  727. int vfs_fchown(struct file *file, uid_t user, gid_t group)
  728. {
  729. int error;
  730. error = mnt_want_write_file(file);
  731. if (error)
  732. return error;
  733. audit_file(file);
  734. error = chown_common(&file->f_path, user, group);
  735. mnt_drop_write_file(file);
  736. return error;
  737. }
  738. int ksys_fchown(unsigned int fd, uid_t user, gid_t group)
  739. {
  740. struct fd f = fdget(fd);
  741. int error = -EBADF;
  742. if (fd_file(f)) {
  743. error = vfs_fchown(fd_file(f), user, group);
  744. fdput(f);
  745. }
  746. return error;
  747. }
  748. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  749. {
  750. return ksys_fchown(fd, user, group);
  751. }
  752. static inline int file_get_write_access(struct file *f)
  753. {
  754. int error;
  755. error = get_write_access(f->f_inode);
  756. if (unlikely(error))
  757. return error;
  758. error = mnt_get_write_access(f->f_path.mnt);
  759. if (unlikely(error))
  760. goto cleanup_inode;
  761. if (unlikely(f->f_mode & FMODE_BACKING)) {
  762. error = mnt_get_write_access(backing_file_user_path(f)->mnt);
  763. if (unlikely(error))
  764. goto cleanup_mnt;
  765. }
  766. return 0;
  767. cleanup_mnt:
  768. mnt_put_write_access(f->f_path.mnt);
  769. cleanup_inode:
  770. put_write_access(f->f_inode);
  771. return error;
  772. }
  773. static int do_dentry_open(struct file *f,
  774. int (*open)(struct inode *, struct file *))
  775. {
  776. static const struct file_operations empty_fops = {};
  777. struct inode *inode = f->f_path.dentry->d_inode;
  778. int error;
  779. path_get(&f->f_path);
  780. f->f_inode = inode;
  781. f->f_mapping = inode->i_mapping;
  782. f->f_wb_err = filemap_sample_wb_err(f->f_mapping);
  783. f->f_sb_err = file_sample_sb_err(f);
  784. if (unlikely(f->f_flags & O_PATH)) {
  785. f->f_mode = FMODE_PATH | FMODE_OPENED;
  786. f->f_op = &empty_fops;
  787. return 0;
  788. }
  789. if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  790. i_readcount_inc(inode);
  791. } else if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
  792. error = file_get_write_access(f);
  793. if (unlikely(error))
  794. goto cleanup_file;
  795. f->f_mode |= FMODE_WRITER;
  796. }
  797. /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
  798. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
  799. f->f_mode |= FMODE_ATOMIC_POS;
  800. f->f_op = fops_get(inode->i_fop);
  801. if (WARN_ON(!f->f_op)) {
  802. error = -ENODEV;
  803. goto cleanup_all;
  804. }
  805. error = security_file_open(f);
  806. if (error)
  807. goto cleanup_all;
  808. error = break_lease(file_inode(f), f->f_flags);
  809. if (error)
  810. goto cleanup_all;
  811. /* normally all 3 are set; ->open() can clear them if needed */
  812. f->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
  813. if (!open)
  814. open = f->f_op->open;
  815. if (open) {
  816. error = open(inode, f);
  817. if (error)
  818. goto cleanup_all;
  819. }
  820. f->f_mode |= FMODE_OPENED;
  821. if ((f->f_mode & FMODE_READ) &&
  822. likely(f->f_op->read || f->f_op->read_iter))
  823. f->f_mode |= FMODE_CAN_READ;
  824. if ((f->f_mode & FMODE_WRITE) &&
  825. likely(f->f_op->write || f->f_op->write_iter))
  826. f->f_mode |= FMODE_CAN_WRITE;
  827. if ((f->f_mode & FMODE_LSEEK) && !f->f_op->llseek)
  828. f->f_mode &= ~FMODE_LSEEK;
  829. if (f->f_mapping->a_ops && f->f_mapping->a_ops->direct_IO)
  830. f->f_mode |= FMODE_CAN_ODIRECT;
  831. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  832. f->f_iocb_flags = iocb_flags(f);
  833. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  834. if ((f->f_flags & O_DIRECT) && !(f->f_mode & FMODE_CAN_ODIRECT))
  835. return -EINVAL;
  836. /*
  837. * XXX: Huge page cache doesn't support writing yet. Drop all page
  838. * cache for this file before processing writes.
  839. */
  840. if (f->f_mode & FMODE_WRITE) {
  841. /*
  842. * Depends on full fence from get_write_access() to synchronize
  843. * against collapse_file() regarding i_writecount and nr_thps
  844. * updates. Ensures subsequent insertion of THPs into the page
  845. * cache will fail.
  846. */
  847. if (filemap_nr_thps(inode->i_mapping)) {
  848. struct address_space *mapping = inode->i_mapping;
  849. filemap_invalidate_lock(inode->i_mapping);
  850. /*
  851. * unmap_mapping_range just need to be called once
  852. * here, because the private pages is not need to be
  853. * unmapped mapping (e.g. data segment of dynamic
  854. * shared libraries here).
  855. */
  856. unmap_mapping_range(mapping, 0, 0, 0);
  857. truncate_inode_pages(mapping, 0);
  858. filemap_invalidate_unlock(inode->i_mapping);
  859. }
  860. }
  861. return 0;
  862. cleanup_all:
  863. if (WARN_ON_ONCE(error > 0))
  864. error = -EINVAL;
  865. fops_put(f->f_op);
  866. put_file_access(f);
  867. cleanup_file:
  868. path_put(&f->f_path);
  869. f->f_path.mnt = NULL;
  870. f->f_path.dentry = NULL;
  871. f->f_inode = NULL;
  872. return error;
  873. }
  874. /**
  875. * finish_open - finish opening a file
  876. * @file: file pointer
  877. * @dentry: pointer to dentry
  878. * @open: open callback
  879. *
  880. * This can be used to finish opening a file passed to i_op->atomic_open().
  881. *
  882. * If the open callback is set to NULL, then the standard f_op->open()
  883. * filesystem callback is substituted.
  884. *
  885. * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
  886. * the return value of d_splice_alias(), then the caller needs to perform dput()
  887. * on it after finish_open().
  888. *
  889. * Returns zero on success or -errno if the open failed.
  890. */
  891. int finish_open(struct file *file, struct dentry *dentry,
  892. int (*open)(struct inode *, struct file *))
  893. {
  894. BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
  895. file->f_path.dentry = dentry;
  896. return do_dentry_open(file, open);
  897. }
  898. EXPORT_SYMBOL(finish_open);
  899. /**
  900. * finish_no_open - finish ->atomic_open() without opening the file
  901. *
  902. * @file: file pointer
  903. * @dentry: dentry or NULL (as returned from ->lookup())
  904. *
  905. * This can be used to set the result of a successful lookup in ->atomic_open().
  906. *
  907. * NB: unlike finish_open() this function does consume the dentry reference and
  908. * the caller need not dput() it.
  909. *
  910. * Returns "0" which must be the return value of ->atomic_open() after having
  911. * called this function.
  912. */
  913. int finish_no_open(struct file *file, struct dentry *dentry)
  914. {
  915. file->f_path.dentry = dentry;
  916. return 0;
  917. }
  918. EXPORT_SYMBOL(finish_no_open);
  919. char *file_path(struct file *filp, char *buf, int buflen)
  920. {
  921. return d_path(&filp->f_path, buf, buflen);
  922. }
  923. EXPORT_SYMBOL(file_path);
  924. /**
  925. * vfs_open - open the file at the given path
  926. * @path: path to open
  927. * @file: newly allocated file with f_flag initialized
  928. */
  929. int vfs_open(const struct path *path, struct file *file)
  930. {
  931. int ret;
  932. file->f_path = *path;
  933. ret = do_dentry_open(file, NULL);
  934. if (!ret) {
  935. /*
  936. * Once we return a file with FMODE_OPENED, __fput() will call
  937. * fsnotify_close(), so we need fsnotify_open() here for
  938. * symmetry.
  939. */
  940. fsnotify_open(file);
  941. }
  942. return ret;
  943. }
  944. struct file *dentry_open(const struct path *path, int flags,
  945. const struct cred *cred)
  946. {
  947. int error;
  948. struct file *f;
  949. /* We must always pass in a valid mount pointer. */
  950. BUG_ON(!path->mnt);
  951. f = alloc_empty_file(flags, cred);
  952. if (!IS_ERR(f)) {
  953. error = vfs_open(path, f);
  954. if (error) {
  955. fput(f);
  956. f = ERR_PTR(error);
  957. }
  958. }
  959. return f;
  960. }
  961. EXPORT_SYMBOL(dentry_open);
  962. /**
  963. * dentry_create - Create and open a file
  964. * @path: path to create
  965. * @flags: O_ flags
  966. * @mode: mode bits for new file
  967. * @cred: credentials to use
  968. *
  969. * Caller must hold the parent directory's lock, and have prepared
  970. * a negative dentry, placed in @path->dentry, for the new file.
  971. *
  972. * Caller sets @path->mnt to the vfsmount of the filesystem where
  973. * the new file is to be created. The parent directory and the
  974. * negative dentry must reside on the same filesystem instance.
  975. *
  976. * On success, returns a "struct file *". Otherwise a ERR_PTR
  977. * is returned.
  978. */
  979. struct file *dentry_create(const struct path *path, int flags, umode_t mode,
  980. const struct cred *cred)
  981. {
  982. struct file *f;
  983. int error;
  984. f = alloc_empty_file(flags, cred);
  985. if (IS_ERR(f))
  986. return f;
  987. error = vfs_create(mnt_idmap(path->mnt),
  988. d_inode(path->dentry->d_parent),
  989. path->dentry, mode, true);
  990. if (!error)
  991. error = vfs_open(path, f);
  992. if (unlikely(error)) {
  993. fput(f);
  994. return ERR_PTR(error);
  995. }
  996. return f;
  997. }
  998. EXPORT_SYMBOL(dentry_create);
  999. /**
  1000. * kernel_file_open - open a file for kernel internal use
  1001. * @path: path of the file to open
  1002. * @flags: open flags
  1003. * @cred: credentials for open
  1004. *
  1005. * Open a file for use by in-kernel consumers. The file is not accounted
  1006. * against nr_files and must not be installed into the file descriptor
  1007. * table.
  1008. *
  1009. * Return: Opened file on success, an error pointer on failure.
  1010. */
  1011. struct file *kernel_file_open(const struct path *path, int flags,
  1012. const struct cred *cred)
  1013. {
  1014. struct file *f;
  1015. int error;
  1016. f = alloc_empty_file_noaccount(flags, cred);
  1017. if (IS_ERR(f))
  1018. return f;
  1019. f->f_path = *path;
  1020. error = do_dentry_open(f, NULL);
  1021. if (error) {
  1022. fput(f);
  1023. return ERR_PTR(error);
  1024. }
  1025. fsnotify_open(f);
  1026. return f;
  1027. }
  1028. EXPORT_SYMBOL_GPL(kernel_file_open);
  1029. #define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE))
  1030. #define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC)
  1031. inline struct open_how build_open_how(int flags, umode_t mode)
  1032. {
  1033. struct open_how how = {
  1034. .flags = flags & VALID_OPEN_FLAGS,
  1035. .mode = mode & S_IALLUGO,
  1036. };
  1037. /* O_PATH beats everything else. */
  1038. if (how.flags & O_PATH)
  1039. how.flags &= O_PATH_FLAGS;
  1040. /* Modes should only be set for create-like flags. */
  1041. if (!WILL_CREATE(how.flags))
  1042. how.mode = 0;
  1043. return how;
  1044. }
  1045. inline int build_open_flags(const struct open_how *how, struct open_flags *op)
  1046. {
  1047. u64 flags = how->flags;
  1048. u64 strip = __FMODE_NONOTIFY | O_CLOEXEC;
  1049. int lookup_flags = 0;
  1050. int acc_mode = ACC_MODE(flags);
  1051. BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS),
  1052. "struct open_flags doesn't yet handle flags > 32 bits");
  1053. /*
  1054. * Strip flags that either shouldn't be set by userspace like
  1055. * FMODE_NONOTIFY or that aren't relevant in determining struct
  1056. * open_flags like O_CLOEXEC.
  1057. */
  1058. flags &= ~strip;
  1059. /*
  1060. * Older syscalls implicitly clear all of the invalid flags or argument
  1061. * values before calling build_open_flags(), but openat2(2) checks all
  1062. * of its arguments.
  1063. */
  1064. if (flags & ~VALID_OPEN_FLAGS)
  1065. return -EINVAL;
  1066. if (how->resolve & ~VALID_RESOLVE_FLAGS)
  1067. return -EINVAL;
  1068. /* Scoping flags are mutually exclusive. */
  1069. if ((how->resolve & RESOLVE_BENEATH) && (how->resolve & RESOLVE_IN_ROOT))
  1070. return -EINVAL;
  1071. /* Deal with the mode. */
  1072. if (WILL_CREATE(flags)) {
  1073. if (how->mode & ~S_IALLUGO)
  1074. return -EINVAL;
  1075. op->mode = how->mode | S_IFREG;
  1076. } else {
  1077. if (how->mode != 0)
  1078. return -EINVAL;
  1079. op->mode = 0;
  1080. }
  1081. /*
  1082. * Block bugs where O_DIRECTORY | O_CREAT created regular files.
  1083. * Note, that blocking O_DIRECTORY | O_CREAT here also protects
  1084. * O_TMPFILE below which requires O_DIRECTORY being raised.
  1085. */
  1086. if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT))
  1087. return -EINVAL;
  1088. /* Now handle the creative implementation of O_TMPFILE. */
  1089. if (flags & __O_TMPFILE) {
  1090. /*
  1091. * In order to ensure programs get explicit errors when trying
  1092. * to use O_TMPFILE on old kernels we enforce that O_DIRECTORY
  1093. * is raised alongside __O_TMPFILE.
  1094. */
  1095. if (!(flags & O_DIRECTORY))
  1096. return -EINVAL;
  1097. if (!(acc_mode & MAY_WRITE))
  1098. return -EINVAL;
  1099. }
  1100. if (flags & O_PATH) {
  1101. /* O_PATH only permits certain other flags to be set. */
  1102. if (flags & ~O_PATH_FLAGS)
  1103. return -EINVAL;
  1104. acc_mode = 0;
  1105. }
  1106. /*
  1107. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  1108. * check for O_DSYNC if the need any syncing at all we enforce it's
  1109. * always set instead of having to deal with possibly weird behaviour
  1110. * for malicious applications setting only __O_SYNC.
  1111. */
  1112. if (flags & __O_SYNC)
  1113. flags |= O_DSYNC;
  1114. op->open_flag = flags;
  1115. /* O_TRUNC implies we need access checks for write permissions */
  1116. if (flags & O_TRUNC)
  1117. acc_mode |= MAY_WRITE;
  1118. /* Allow the LSM permission hook to distinguish append
  1119. access from general write access. */
  1120. if (flags & O_APPEND)
  1121. acc_mode |= MAY_APPEND;
  1122. op->acc_mode = acc_mode;
  1123. op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
  1124. if (flags & O_CREAT) {
  1125. op->intent |= LOOKUP_CREATE;
  1126. if (flags & O_EXCL) {
  1127. op->intent |= LOOKUP_EXCL;
  1128. flags |= O_NOFOLLOW;
  1129. }
  1130. }
  1131. if (flags & O_DIRECTORY)
  1132. lookup_flags |= LOOKUP_DIRECTORY;
  1133. if (!(flags & O_NOFOLLOW))
  1134. lookup_flags |= LOOKUP_FOLLOW;
  1135. if (how->resolve & RESOLVE_NO_XDEV)
  1136. lookup_flags |= LOOKUP_NO_XDEV;
  1137. if (how->resolve & RESOLVE_NO_MAGICLINKS)
  1138. lookup_flags |= LOOKUP_NO_MAGICLINKS;
  1139. if (how->resolve & RESOLVE_NO_SYMLINKS)
  1140. lookup_flags |= LOOKUP_NO_SYMLINKS;
  1141. if (how->resolve & RESOLVE_BENEATH)
  1142. lookup_flags |= LOOKUP_BENEATH;
  1143. if (how->resolve & RESOLVE_IN_ROOT)
  1144. lookup_flags |= LOOKUP_IN_ROOT;
  1145. if (how->resolve & RESOLVE_CACHED) {
  1146. /* Don't bother even trying for create/truncate/tmpfile open */
  1147. if (flags & (O_TRUNC | O_CREAT | __O_TMPFILE))
  1148. return -EAGAIN;
  1149. lookup_flags |= LOOKUP_CACHED;
  1150. }
  1151. op->lookup_flags = lookup_flags;
  1152. return 0;
  1153. }
  1154. /**
  1155. * file_open_name - open file and return file pointer
  1156. *
  1157. * @name: struct filename containing path to open
  1158. * @flags: open flags as per the open(2) second argument
  1159. * @mode: mode for the new file if O_CREAT is set, else ignored
  1160. *
  1161. * This is the helper to open a file from kernelspace if you really
  1162. * have to. But in generally you should not do this, so please move
  1163. * along, nothing to see here..
  1164. */
  1165. struct file *file_open_name(struct filename *name, int flags, umode_t mode)
  1166. {
  1167. struct open_flags op;
  1168. struct open_how how = build_open_how(flags, mode);
  1169. int err = build_open_flags(&how, &op);
  1170. if (err)
  1171. return ERR_PTR(err);
  1172. return do_filp_open(AT_FDCWD, name, &op);
  1173. }
  1174. /**
  1175. * filp_open - open file and return file pointer
  1176. *
  1177. * @filename: path to open
  1178. * @flags: open flags as per the open(2) second argument
  1179. * @mode: mode for the new file if O_CREAT is set, else ignored
  1180. *
  1181. * This is the helper to open a file from kernelspace if you really
  1182. * have to. But in generally you should not do this, so please move
  1183. * along, nothing to see here..
  1184. */
  1185. struct file *filp_open(const char *filename, int flags, umode_t mode)
  1186. {
  1187. struct filename *name = getname_kernel(filename);
  1188. struct file *file = ERR_CAST(name);
  1189. if (!IS_ERR(name)) {
  1190. file = file_open_name(name, flags, mode);
  1191. putname(name);
  1192. }
  1193. return file;
  1194. }
  1195. EXPORT_SYMBOL(filp_open);
  1196. struct file *file_open_root(const struct path *root,
  1197. const char *filename, int flags, umode_t mode)
  1198. {
  1199. struct open_flags op;
  1200. struct open_how how = build_open_how(flags, mode);
  1201. int err = build_open_flags(&how, &op);
  1202. if (err)
  1203. return ERR_PTR(err);
  1204. return do_file_open_root(root, filename, &op);
  1205. }
  1206. EXPORT_SYMBOL(file_open_root);
  1207. static long do_sys_openat2(int dfd, const char __user *filename,
  1208. struct open_how *how)
  1209. {
  1210. struct open_flags op;
  1211. int fd = build_open_flags(how, &op);
  1212. struct filename *tmp;
  1213. if (fd)
  1214. return fd;
  1215. tmp = getname(filename);
  1216. if (IS_ERR(tmp))
  1217. return PTR_ERR(tmp);
  1218. fd = get_unused_fd_flags(how->flags);
  1219. if (fd >= 0) {
  1220. struct file *f = do_filp_open(dfd, tmp, &op);
  1221. if (IS_ERR(f)) {
  1222. put_unused_fd(fd);
  1223. fd = PTR_ERR(f);
  1224. } else {
  1225. fd_install(fd, f);
  1226. }
  1227. }
  1228. putname(tmp);
  1229. return fd;
  1230. }
  1231. long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
  1232. {
  1233. struct open_how how = build_open_how(flags, mode);
  1234. return do_sys_openat2(dfd, filename, &how);
  1235. }
  1236. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  1237. {
  1238. if (force_o_largefile())
  1239. flags |= O_LARGEFILE;
  1240. return do_sys_open(AT_FDCWD, filename, flags, mode);
  1241. }
  1242. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  1243. umode_t, mode)
  1244. {
  1245. if (force_o_largefile())
  1246. flags |= O_LARGEFILE;
  1247. return do_sys_open(dfd, filename, flags, mode);
  1248. }
  1249. SYSCALL_DEFINE4(openat2, int, dfd, const char __user *, filename,
  1250. struct open_how __user *, how, size_t, usize)
  1251. {
  1252. int err;
  1253. struct open_how tmp;
  1254. BUILD_BUG_ON(sizeof(struct open_how) < OPEN_HOW_SIZE_VER0);
  1255. BUILD_BUG_ON(sizeof(struct open_how) != OPEN_HOW_SIZE_LATEST);
  1256. if (unlikely(usize < OPEN_HOW_SIZE_VER0))
  1257. return -EINVAL;
  1258. if (unlikely(usize > PAGE_SIZE))
  1259. return -E2BIG;
  1260. err = copy_struct_from_user(&tmp, sizeof(tmp), how, usize);
  1261. if (err)
  1262. return err;
  1263. audit_openat2_how(&tmp);
  1264. /* O_LARGEFILE is only allowed for non-O_PATH. */
  1265. if (!(tmp.flags & O_PATH) && force_o_largefile())
  1266. tmp.flags |= O_LARGEFILE;
  1267. return do_sys_openat2(dfd, filename, &tmp);
  1268. }
  1269. #ifdef CONFIG_COMPAT
  1270. /*
  1271. * Exactly like sys_open(), except that it doesn't set the
  1272. * O_LARGEFILE flag.
  1273. */
  1274. COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  1275. {
  1276. return do_sys_open(AT_FDCWD, filename, flags, mode);
  1277. }
  1278. /*
  1279. * Exactly like sys_openat(), except that it doesn't set the
  1280. * O_LARGEFILE flag.
  1281. */
  1282. COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
  1283. {
  1284. return do_sys_open(dfd, filename, flags, mode);
  1285. }
  1286. #endif
  1287. #ifndef __alpha__
  1288. /*
  1289. * For backward compatibility? Maybe this should be moved
  1290. * into arch/i386 instead?
  1291. */
  1292. SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
  1293. {
  1294. int flags = O_CREAT | O_WRONLY | O_TRUNC;
  1295. if (force_o_largefile())
  1296. flags |= O_LARGEFILE;
  1297. return do_sys_open(AT_FDCWD, pathname, flags, mode);
  1298. }
  1299. #endif
  1300. /*
  1301. * "id" is the POSIX thread ID. We use the
  1302. * files pointer for this..
  1303. */
  1304. static int filp_flush(struct file *filp, fl_owner_t id)
  1305. {
  1306. int retval = 0;
  1307. if (CHECK_DATA_CORRUPTION(file_count(filp) == 0,
  1308. "VFS: Close: file count is 0 (f_op=%ps)",
  1309. filp->f_op)) {
  1310. return 0;
  1311. }
  1312. if (filp->f_op->flush)
  1313. retval = filp->f_op->flush(filp, id);
  1314. if (likely(!(filp->f_mode & FMODE_PATH))) {
  1315. dnotify_flush(filp, id);
  1316. locks_remove_posix(filp, id);
  1317. }
  1318. return retval;
  1319. }
  1320. int filp_close(struct file *filp, fl_owner_t id)
  1321. {
  1322. int retval;
  1323. retval = filp_flush(filp, id);
  1324. fput(filp);
  1325. return retval;
  1326. }
  1327. EXPORT_SYMBOL(filp_close);
  1328. /*
  1329. * Careful here! We test whether the file pointer is NULL before
  1330. * releasing the fd. This ensures that one clone task can't release
  1331. * an fd while another clone is opening it.
  1332. */
  1333. SYSCALL_DEFINE1(close, unsigned int, fd)
  1334. {
  1335. int retval;
  1336. struct file *file;
  1337. file = file_close_fd(fd);
  1338. if (!file)
  1339. return -EBADF;
  1340. retval = filp_flush(file, current->files);
  1341. /*
  1342. * We're returning to user space. Don't bother
  1343. * with any delayed fput() cases.
  1344. */
  1345. __fput_sync(file);
  1346. /* can't restart close syscall because file table entry was cleared */
  1347. if (unlikely(retval == -ERESTARTSYS ||
  1348. retval == -ERESTARTNOINTR ||
  1349. retval == -ERESTARTNOHAND ||
  1350. retval == -ERESTART_RESTARTBLOCK))
  1351. retval = -EINTR;
  1352. return retval;
  1353. }
  1354. /**
  1355. * sys_close_range() - Close all file descriptors in a given range.
  1356. *
  1357. * @fd: starting file descriptor to close
  1358. * @max_fd: last file descriptor to close
  1359. * @flags: reserved for future extensions
  1360. *
  1361. * This closes a range of file descriptors. All file descriptors
  1362. * from @fd up to and including @max_fd are closed.
  1363. * Currently, errors to close a given file descriptor are ignored.
  1364. */
  1365. SYSCALL_DEFINE3(close_range, unsigned int, fd, unsigned int, max_fd,
  1366. unsigned int, flags)
  1367. {
  1368. return __close_range(fd, max_fd, flags);
  1369. }
  1370. /*
  1371. * This routine simulates a hangup on the tty, to arrange that users
  1372. * are given clean terminals at login time.
  1373. */
  1374. SYSCALL_DEFINE0(vhangup)
  1375. {
  1376. if (capable(CAP_SYS_TTY_CONFIG)) {
  1377. tty_vhangup_self();
  1378. return 0;
  1379. }
  1380. return -EPERM;
  1381. }
  1382. /*
  1383. * Called when an inode is about to be open.
  1384. * We use this to disallow opening large files on 32bit systems if
  1385. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  1386. * on this flag in sys_open.
  1387. */
  1388. int generic_file_open(struct inode * inode, struct file * filp)
  1389. {
  1390. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  1391. return -EOVERFLOW;
  1392. return 0;
  1393. }
  1394. EXPORT_SYMBOL(generic_file_open);
  1395. /*
  1396. * This is used by subsystems that don't want seekable
  1397. * file descriptors. The function is not supposed to ever fail, the only
  1398. * reason it returns an 'int' and not 'void' is so that it can be plugged
  1399. * directly into file_operations structure.
  1400. */
  1401. int nonseekable_open(struct inode *inode, struct file *filp)
  1402. {
  1403. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1404. return 0;
  1405. }
  1406. EXPORT_SYMBOL(nonseekable_open);
  1407. /*
  1408. * stream_open is used by subsystems that want stream-like file descriptors.
  1409. * Such file descriptors are not seekable and don't have notion of position
  1410. * (file.f_pos is always 0 and ppos passed to .read()/.write() is always NULL).
  1411. * Contrary to file descriptors of other regular files, .read() and .write()
  1412. * can run simultaneously.
  1413. *
  1414. * stream_open never fails and is marked to return int so that it could be
  1415. * directly used as file_operations.open .
  1416. */
  1417. int stream_open(struct inode *inode, struct file *filp)
  1418. {
  1419. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS);
  1420. filp->f_mode |= FMODE_STREAM;
  1421. return 0;
  1422. }
  1423. EXPORT_SYMBOL(stream_open);