open.c 38 KB

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