open.c 39 KB

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