open.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. /*
  2. * linux/fs/open.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/string.h>
  7. #include <linux/mm.h>
  8. #include <linux/file.h>
  9. #include <linux/fdtable.h>
  10. #include <linux/quotaops.h>
  11. #include <linux/fsnotify.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/tty.h>
  15. #include <linux/namei.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/capability.h>
  18. #include <linux/securebits.h>
  19. #include <linux/security.h>
  20. #include <linux/mount.h>
  21. #include <linux/vfs.h>
  22. #include <linux/fcntl.h>
  23. #include <asm/uaccess.h>
  24. #include <linux/fs.h>
  25. #include <linux/personality.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/syscalls.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/audit.h>
  30. #include <linux/falloc.h>
  31. #include <linux/fs_struct.h>
  32. #include <linux/ima.h>
  33. #include "internal.h"
  34. int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  35. {
  36. int retval = -ENODEV;
  37. if (dentry) {
  38. retval = -ENOSYS;
  39. if (dentry->d_sb->s_op->statfs) {
  40. memset(buf, 0, sizeof(*buf));
  41. retval = security_sb_statfs(dentry);
  42. if (retval)
  43. return retval;
  44. retval = dentry->d_sb->s_op->statfs(dentry, buf);
  45. if (retval == 0 && buf->f_frsize == 0)
  46. buf->f_frsize = buf->f_bsize;
  47. }
  48. }
  49. return retval;
  50. }
  51. EXPORT_SYMBOL(vfs_statfs);
  52. static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)
  53. {
  54. struct kstatfs st;
  55. int retval;
  56. retval = vfs_statfs(dentry, &st);
  57. if (retval)
  58. return retval;
  59. if (sizeof(*buf) == sizeof(st))
  60. memcpy(buf, &st, sizeof(st));
  61. else {
  62. if (sizeof buf->f_blocks == 4) {
  63. if ((st.f_blocks | st.f_bfree | st.f_bavail |
  64. st.f_bsize | st.f_frsize) &
  65. 0xffffffff00000000ULL)
  66. return -EOVERFLOW;
  67. /*
  68. * f_files and f_ffree may be -1; it's okay to stuff
  69. * that into 32 bits
  70. */
  71. if (st.f_files != -1 &&
  72. (st.f_files & 0xffffffff00000000ULL))
  73. return -EOVERFLOW;
  74. if (st.f_ffree != -1 &&
  75. (st.f_ffree & 0xffffffff00000000ULL))
  76. return -EOVERFLOW;
  77. }
  78. buf->f_type = st.f_type;
  79. buf->f_bsize = st.f_bsize;
  80. buf->f_blocks = st.f_blocks;
  81. buf->f_bfree = st.f_bfree;
  82. buf->f_bavail = st.f_bavail;
  83. buf->f_files = st.f_files;
  84. buf->f_ffree = st.f_ffree;
  85. buf->f_fsid = st.f_fsid;
  86. buf->f_namelen = st.f_namelen;
  87. buf->f_frsize = st.f_frsize;
  88. memset(buf->f_spare, 0, sizeof(buf->f_spare));
  89. }
  90. return 0;
  91. }
  92. static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
  93. {
  94. struct kstatfs st;
  95. int retval;
  96. retval = vfs_statfs(dentry, &st);
  97. if (retval)
  98. return retval;
  99. if (sizeof(*buf) == sizeof(st))
  100. memcpy(buf, &st, sizeof(st));
  101. else {
  102. buf->f_type = st.f_type;
  103. buf->f_bsize = st.f_bsize;
  104. buf->f_blocks = st.f_blocks;
  105. buf->f_bfree = st.f_bfree;
  106. buf->f_bavail = st.f_bavail;
  107. buf->f_files = st.f_files;
  108. buf->f_ffree = st.f_ffree;
  109. buf->f_fsid = st.f_fsid;
  110. buf->f_namelen = st.f_namelen;
  111. buf->f_frsize = st.f_frsize;
  112. memset(buf->f_spare, 0, sizeof(buf->f_spare));
  113. }
  114. return 0;
  115. }
  116. SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
  117. {
  118. struct path path;
  119. int error;
  120. error = user_path(pathname, &path);
  121. if (!error) {
  122. struct statfs tmp;
  123. error = vfs_statfs_native(path.dentry, &tmp);
  124. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  125. error = -EFAULT;
  126. path_put(&path);
  127. }
  128. return error;
  129. }
  130. SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
  131. {
  132. struct path path;
  133. long error;
  134. if (sz != sizeof(*buf))
  135. return -EINVAL;
  136. error = user_path(pathname, &path);
  137. if (!error) {
  138. struct statfs64 tmp;
  139. error = vfs_statfs64(path.dentry, &tmp);
  140. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  141. error = -EFAULT;
  142. path_put(&path);
  143. }
  144. return error;
  145. }
  146. SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
  147. {
  148. struct file * file;
  149. struct statfs tmp;
  150. int error;
  151. error = -EBADF;
  152. file = fget(fd);
  153. if (!file)
  154. goto out;
  155. error = vfs_statfs_native(file->f_path.dentry, &tmp);
  156. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  157. error = -EFAULT;
  158. fput(file);
  159. out:
  160. return error;
  161. }
  162. SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
  163. {
  164. struct file * file;
  165. struct statfs64 tmp;
  166. int error;
  167. if (sz != sizeof(*buf))
  168. return -EINVAL;
  169. error = -EBADF;
  170. file = fget(fd);
  171. if (!file)
  172. goto out;
  173. error = vfs_statfs64(file->f_path.dentry, &tmp);
  174. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  175. error = -EFAULT;
  176. fput(file);
  177. out:
  178. return error;
  179. }
  180. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  181. struct file *filp)
  182. {
  183. int ret;
  184. struct iattr newattrs;
  185. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  186. if (length < 0)
  187. return -EINVAL;
  188. newattrs.ia_size = length;
  189. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  190. if (filp) {
  191. newattrs.ia_file = filp;
  192. newattrs.ia_valid |= ATTR_FILE;
  193. }
  194. /* Remove suid/sgid on truncate too */
  195. ret = should_remove_suid(dentry);
  196. if (ret)
  197. newattrs.ia_valid |= ret | ATTR_FORCE;
  198. mutex_lock(&dentry->d_inode->i_mutex);
  199. ret = notify_change(dentry, &newattrs);
  200. mutex_unlock(&dentry->d_inode->i_mutex);
  201. return ret;
  202. }
  203. static long do_sys_truncate(const char __user *pathname, loff_t length)
  204. {
  205. struct path path;
  206. struct inode *inode;
  207. int error;
  208. error = -EINVAL;
  209. if (length < 0) /* sorry, but loff_t says... */
  210. goto out;
  211. error = user_path(pathname, &path);
  212. if (error)
  213. goto out;
  214. inode = path.dentry->d_inode;
  215. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  216. error = -EISDIR;
  217. if (S_ISDIR(inode->i_mode))
  218. goto dput_and_out;
  219. error = -EINVAL;
  220. if (!S_ISREG(inode->i_mode))
  221. goto dput_and_out;
  222. error = mnt_want_write(path.mnt);
  223. if (error)
  224. goto dput_and_out;
  225. error = inode_permission(inode, MAY_WRITE);
  226. if (error)
  227. goto mnt_drop_write_and_out;
  228. error = -EPERM;
  229. if (IS_APPEND(inode))
  230. goto mnt_drop_write_and_out;
  231. error = get_write_access(inode);
  232. if (error)
  233. goto mnt_drop_write_and_out;
  234. /*
  235. * Make sure that there are no leases. get_write_access() protects
  236. * against the truncate racing with a lease-granting setlease().
  237. */
  238. error = break_lease(inode, FMODE_WRITE);
  239. if (error)
  240. goto put_write_and_out;
  241. error = locks_verify_truncate(inode, NULL, length);
  242. if (!error)
  243. error = security_path_truncate(&path, length, 0);
  244. if (!error) {
  245. vfs_dq_init(inode);
  246. error = do_truncate(path.dentry, length, 0, NULL);
  247. }
  248. put_write_and_out:
  249. put_write_access(inode);
  250. mnt_drop_write_and_out:
  251. mnt_drop_write(path.mnt);
  252. dput_and_out:
  253. path_put(&path);
  254. out:
  255. return error;
  256. }
  257. SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
  258. {
  259. return do_sys_truncate(path, length);
  260. }
  261. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  262. {
  263. struct inode * inode;
  264. struct dentry *dentry;
  265. struct file * file;
  266. int error;
  267. error = -EINVAL;
  268. if (length < 0)
  269. goto out;
  270. error = -EBADF;
  271. file = fget(fd);
  272. if (!file)
  273. goto out;
  274. /* explicitly opened as large or we are on 64-bit box */
  275. if (file->f_flags & O_LARGEFILE)
  276. small = 0;
  277. dentry = file->f_path.dentry;
  278. inode = dentry->d_inode;
  279. error = -EINVAL;
  280. if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
  281. goto out_putf;
  282. error = -EINVAL;
  283. /* Cannot ftruncate over 2^31 bytes without large file support */
  284. if (small && length > MAX_NON_LFS)
  285. goto out_putf;
  286. error = -EPERM;
  287. if (IS_APPEND(inode))
  288. goto out_putf;
  289. error = locks_verify_truncate(inode, file, length);
  290. if (!error)
  291. error = security_path_truncate(&file->f_path, length,
  292. ATTR_MTIME|ATTR_CTIME);
  293. if (!error)
  294. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
  295. out_putf:
  296. fput(file);
  297. out:
  298. return error;
  299. }
  300. SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
  301. {
  302. long ret = do_sys_ftruncate(fd, length, 1);
  303. /* avoid REGPARM breakage on x86: */
  304. asmlinkage_protect(2, ret, fd, length);
  305. return ret;
  306. }
  307. /* LFS versions of truncate are only needed on 32 bit machines */
  308. #if BITS_PER_LONG == 32
  309. SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length)
  310. {
  311. return do_sys_truncate(path, length);
  312. }
  313. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  314. asmlinkage long SyS_truncate64(long path, loff_t length)
  315. {
  316. return SYSC_truncate64((const char __user *) path, length);
  317. }
  318. SYSCALL_ALIAS(sys_truncate64, SyS_truncate64);
  319. #endif
  320. SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length)
  321. {
  322. long ret = do_sys_ftruncate(fd, length, 0);
  323. /* avoid REGPARM breakage on x86: */
  324. asmlinkage_protect(2, ret, fd, length);
  325. return ret;
  326. }
  327. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  328. asmlinkage long SyS_ftruncate64(long fd, loff_t length)
  329. {
  330. return SYSC_ftruncate64((unsigned int) fd, length);
  331. }
  332. SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64);
  333. #endif
  334. #endif /* BITS_PER_LONG == 32 */
  335. int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  336. {
  337. struct inode *inode = file->f_path.dentry->d_inode;
  338. long ret;
  339. if (offset < 0 || len <= 0)
  340. return -EINVAL;
  341. /* Return error if mode is not supported */
  342. if (mode && !(mode & FALLOC_FL_KEEP_SIZE))
  343. return -EOPNOTSUPP;
  344. if (!(file->f_mode & FMODE_WRITE))
  345. return -EBADF;
  346. /*
  347. * Revalidate the write permissions, in case security policy has
  348. * changed since the files were opened.
  349. */
  350. ret = security_file_permission(file, MAY_WRITE);
  351. if (ret)
  352. return ret;
  353. if (S_ISFIFO(inode->i_mode))
  354. return -ESPIPE;
  355. /*
  356. * Let individual file system decide if it supports preallocation
  357. * for directories or not.
  358. */
  359. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  360. return -ENODEV;
  361. /* Check for wrap through zero too */
  362. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  363. return -EFBIG;
  364. if (!inode->i_op->fallocate)
  365. return -EOPNOTSUPP;
  366. return inode->i_op->fallocate(inode, mode, offset, len);
  367. }
  368. SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len)
  369. {
  370. struct file *file;
  371. int error = -EBADF;
  372. file = fget(fd);
  373. if (file) {
  374. error = do_fallocate(file, mode, offset, len);
  375. fput(file);
  376. }
  377. return error;
  378. }
  379. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  380. asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len)
  381. {
  382. return SYSC_fallocate((int)fd, (int)mode, offset, len);
  383. }
  384. SYSCALL_ALIAS(sys_fallocate, SyS_fallocate);
  385. #endif
  386. /*
  387. * access() needs to use the real uid/gid, not the effective uid/gid.
  388. * We do this by temporarily clearing all FS-related capabilities and
  389. * switching the fsuid/fsgid around to the real ones.
  390. */
  391. SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
  392. {
  393. const struct cred *old_cred;
  394. struct cred *override_cred;
  395. struct path path;
  396. struct inode *inode;
  397. int res;
  398. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  399. return -EINVAL;
  400. override_cred = prepare_creds();
  401. if (!override_cred)
  402. return -ENOMEM;
  403. override_cred->fsuid = override_cred->uid;
  404. override_cred->fsgid = override_cred->gid;
  405. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  406. /* Clear the capabilities if we switch to a non-root user */
  407. if (override_cred->uid)
  408. cap_clear(override_cred->cap_effective);
  409. else
  410. override_cred->cap_effective =
  411. override_cred->cap_permitted;
  412. }
  413. old_cred = override_creds(override_cred);
  414. res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  415. if (res)
  416. goto out;
  417. inode = path.dentry->d_inode;
  418. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  419. /*
  420. * MAY_EXEC on regular files is denied if the fs is mounted
  421. * with the "noexec" flag.
  422. */
  423. res = -EACCES;
  424. if (path.mnt->mnt_flags & MNT_NOEXEC)
  425. goto out_path_release;
  426. }
  427. res = inode_permission(inode, mode | MAY_ACCESS);
  428. /* SuS v2 requires we report a read only fs too */
  429. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  430. goto out_path_release;
  431. /*
  432. * This is a rare case where using __mnt_is_readonly()
  433. * is OK without a mnt_want/drop_write() pair. Since
  434. * no actual write to the fs is performed here, we do
  435. * not need to telegraph to that to anyone.
  436. *
  437. * By doing this, we accept that this access is
  438. * inherently racy and know that the fs may change
  439. * state before we even see this result.
  440. */
  441. if (__mnt_is_readonly(path.mnt))
  442. res = -EROFS;
  443. out_path_release:
  444. path_put(&path);
  445. out:
  446. revert_creds(old_cred);
  447. put_cred(override_cred);
  448. return res;
  449. }
  450. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  451. {
  452. return sys_faccessat(AT_FDCWD, filename, mode);
  453. }
  454. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  455. {
  456. struct path path;
  457. int error;
  458. error = user_path_dir(filename, &path);
  459. if (error)
  460. goto out;
  461. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
  462. if (error)
  463. goto dput_and_out;
  464. set_fs_pwd(current->fs, &path);
  465. dput_and_out:
  466. path_put(&path);
  467. out:
  468. return error;
  469. }
  470. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  471. {
  472. struct file *file;
  473. struct inode *inode;
  474. int error;
  475. error = -EBADF;
  476. file = fget(fd);
  477. if (!file)
  478. goto out;
  479. inode = file->f_path.dentry->d_inode;
  480. error = -ENOTDIR;
  481. if (!S_ISDIR(inode->i_mode))
  482. goto out_putf;
  483. error = inode_permission(inode, MAY_EXEC | MAY_ACCESS);
  484. if (!error)
  485. set_fs_pwd(current->fs, &file->f_path);
  486. out_putf:
  487. fput(file);
  488. out:
  489. return error;
  490. }
  491. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  492. {
  493. struct path path;
  494. int error;
  495. error = user_path_dir(filename, &path);
  496. if (error)
  497. goto out;
  498. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
  499. if (error)
  500. goto dput_and_out;
  501. error = -EPERM;
  502. if (!capable(CAP_SYS_CHROOT))
  503. goto dput_and_out;
  504. error = security_path_chroot(&path);
  505. if (error)
  506. goto dput_and_out;
  507. set_fs_root(current->fs, &path);
  508. error = 0;
  509. dput_and_out:
  510. path_put(&path);
  511. out:
  512. return error;
  513. }
  514. SYSCALL_DEFINE2(fchmod, unsigned int, fd, mode_t, mode)
  515. {
  516. struct inode * inode;
  517. struct dentry * dentry;
  518. struct file * file;
  519. int err = -EBADF;
  520. struct iattr newattrs;
  521. file = fget(fd);
  522. if (!file)
  523. goto out;
  524. dentry = file->f_path.dentry;
  525. inode = dentry->d_inode;
  526. audit_inode(NULL, dentry);
  527. err = mnt_want_write_file(file);
  528. if (err)
  529. goto out_putf;
  530. mutex_lock(&inode->i_mutex);
  531. err = security_path_chmod(dentry, file->f_vfsmnt, mode);
  532. if (err)
  533. goto out_unlock;
  534. if (mode == (mode_t) -1)
  535. mode = inode->i_mode;
  536. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  537. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  538. err = notify_change(dentry, &newattrs);
  539. out_unlock:
  540. mutex_unlock(&inode->i_mutex);
  541. mnt_drop_write(file->f_path.mnt);
  542. out_putf:
  543. fput(file);
  544. out:
  545. return err;
  546. }
  547. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, mode_t, mode)
  548. {
  549. struct path path;
  550. struct inode *inode;
  551. int error;
  552. struct iattr newattrs;
  553. error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  554. if (error)
  555. goto out;
  556. inode = path.dentry->d_inode;
  557. error = mnt_want_write(path.mnt);
  558. if (error)
  559. goto dput_and_out;
  560. mutex_lock(&inode->i_mutex);
  561. error = security_path_chmod(path.dentry, path.mnt, mode);
  562. if (error)
  563. goto out_unlock;
  564. if (mode == (mode_t) -1)
  565. mode = inode->i_mode;
  566. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  567. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  568. error = notify_change(path.dentry, &newattrs);
  569. out_unlock:
  570. mutex_unlock(&inode->i_mutex);
  571. mnt_drop_write(path.mnt);
  572. dput_and_out:
  573. path_put(&path);
  574. out:
  575. return error;
  576. }
  577. SYSCALL_DEFINE2(chmod, const char __user *, filename, mode_t, mode)
  578. {
  579. return sys_fchmodat(AT_FDCWD, filename, mode);
  580. }
  581. static int chown_common(struct path *path, uid_t user, gid_t group)
  582. {
  583. struct inode *inode = path->dentry->d_inode;
  584. int error;
  585. struct iattr newattrs;
  586. newattrs.ia_valid = ATTR_CTIME;
  587. if (user != (uid_t) -1) {
  588. newattrs.ia_valid |= ATTR_UID;
  589. newattrs.ia_uid = user;
  590. }
  591. if (group != (gid_t) -1) {
  592. newattrs.ia_valid |= ATTR_GID;
  593. newattrs.ia_gid = group;
  594. }
  595. if (!S_ISDIR(inode->i_mode))
  596. newattrs.ia_valid |=
  597. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  598. mutex_lock(&inode->i_mutex);
  599. error = security_path_chown(path, user, group);
  600. if (!error)
  601. error = notify_change(path->dentry, &newattrs);
  602. mutex_unlock(&inode->i_mutex);
  603. return error;
  604. }
  605. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  606. {
  607. struct path path;
  608. int error;
  609. error = user_path(filename, &path);
  610. if (error)
  611. goto out;
  612. error = mnt_want_write(path.mnt);
  613. if (error)
  614. goto out_release;
  615. error = chown_common(&path, user, group);
  616. mnt_drop_write(path.mnt);
  617. out_release:
  618. path_put(&path);
  619. out:
  620. return error;
  621. }
  622. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  623. gid_t, group, int, flag)
  624. {
  625. struct path path;
  626. int error = -EINVAL;
  627. int follow;
  628. if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
  629. goto out;
  630. follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  631. error = user_path_at(dfd, filename, follow, &path);
  632. if (error)
  633. goto out;
  634. error = mnt_want_write(path.mnt);
  635. if (error)
  636. goto out_release;
  637. error = chown_common(&path, user, group);
  638. mnt_drop_write(path.mnt);
  639. out_release:
  640. path_put(&path);
  641. out:
  642. return error;
  643. }
  644. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  645. {
  646. struct path path;
  647. int error;
  648. error = user_lpath(filename, &path);
  649. if (error)
  650. goto out;
  651. error = mnt_want_write(path.mnt);
  652. if (error)
  653. goto out_release;
  654. error = chown_common(&path, user, group);
  655. mnt_drop_write(path.mnt);
  656. out_release:
  657. path_put(&path);
  658. out:
  659. return error;
  660. }
  661. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  662. {
  663. struct file * file;
  664. int error = -EBADF;
  665. struct dentry * dentry;
  666. file = fget(fd);
  667. if (!file)
  668. goto out;
  669. error = mnt_want_write_file(file);
  670. if (error)
  671. goto out_fput;
  672. dentry = file->f_path.dentry;
  673. audit_inode(NULL, dentry);
  674. error = chown_common(&file->f_path, user, group);
  675. mnt_drop_write(file->f_path.mnt);
  676. out_fput:
  677. fput(file);
  678. out:
  679. return error;
  680. }
  681. /*
  682. * You have to be very careful that these write
  683. * counts get cleaned up in error cases and
  684. * upon __fput(). This should probably never
  685. * be called outside of __dentry_open().
  686. */
  687. static inline int __get_file_write_access(struct inode *inode,
  688. struct vfsmount *mnt)
  689. {
  690. int error;
  691. error = get_write_access(inode);
  692. if (error)
  693. return error;
  694. /*
  695. * Do not take mount writer counts on
  696. * special files since no writes to
  697. * the mount itself will occur.
  698. */
  699. if (!special_file(inode->i_mode)) {
  700. /*
  701. * Balanced in __fput()
  702. */
  703. error = mnt_want_write(mnt);
  704. if (error)
  705. put_write_access(inode);
  706. }
  707. return error;
  708. }
  709. static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
  710. struct file *f,
  711. int (*open)(struct inode *, struct file *),
  712. const struct cred *cred)
  713. {
  714. struct inode *inode;
  715. int error;
  716. f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
  717. FMODE_PREAD | FMODE_PWRITE;
  718. inode = dentry->d_inode;
  719. if (f->f_mode & FMODE_WRITE) {
  720. error = __get_file_write_access(inode, mnt);
  721. if (error)
  722. goto cleanup_file;
  723. if (!special_file(inode->i_mode))
  724. file_take_write(f);
  725. }
  726. f->f_mapping = inode->i_mapping;
  727. f->f_path.dentry = dentry;
  728. f->f_path.mnt = mnt;
  729. f->f_pos = 0;
  730. f->f_op = fops_get(inode->i_fop);
  731. file_move(f, &inode->i_sb->s_files);
  732. error = security_dentry_open(f, cred);
  733. if (error)
  734. goto cleanup_all;
  735. if (!open && f->f_op)
  736. open = f->f_op->open;
  737. if (open) {
  738. error = open(inode, f);
  739. if (error)
  740. goto cleanup_all;
  741. }
  742. ima_counts_get(f);
  743. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  744. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  745. /* NB: we're sure to have correct a_ops only after f_op->open */
  746. if (f->f_flags & O_DIRECT) {
  747. if (!f->f_mapping->a_ops ||
  748. ((!f->f_mapping->a_ops->direct_IO) &&
  749. (!f->f_mapping->a_ops->get_xip_mem))) {
  750. fput(f);
  751. f = ERR_PTR(-EINVAL);
  752. }
  753. }
  754. return f;
  755. cleanup_all:
  756. fops_put(f->f_op);
  757. if (f->f_mode & FMODE_WRITE) {
  758. put_write_access(inode);
  759. if (!special_file(inode->i_mode)) {
  760. /*
  761. * We don't consider this a real
  762. * mnt_want/drop_write() pair
  763. * because it all happenend right
  764. * here, so just reset the state.
  765. */
  766. file_reset_write(f);
  767. mnt_drop_write(mnt);
  768. }
  769. }
  770. file_kill(f);
  771. f->f_path.dentry = NULL;
  772. f->f_path.mnt = NULL;
  773. cleanup_file:
  774. put_filp(f);
  775. dput(dentry);
  776. mntput(mnt);
  777. return ERR_PTR(error);
  778. }
  779. /**
  780. * lookup_instantiate_filp - instantiates the open intent filp
  781. * @nd: pointer to nameidata
  782. * @dentry: pointer to dentry
  783. * @open: open callback
  784. *
  785. * Helper for filesystems that want to use lookup open intents and pass back
  786. * a fully instantiated struct file to the caller.
  787. * This function is meant to be called from within a filesystem's
  788. * lookup method.
  789. * Beware of calling it for non-regular files! Those ->open methods might block
  790. * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
  791. * leading to a deadlock, as nobody can open that fifo anymore, because
  792. * another process to open fifo will block on locked parent when doing lookup).
  793. * Note that in case of error, nd->intent.open.file is destroyed, but the
  794. * path information remains valid.
  795. * If the open callback is set to NULL, then the standard f_op->open()
  796. * filesystem callback is substituted.
  797. */
  798. struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
  799. int (*open)(struct inode *, struct file *))
  800. {
  801. const struct cred *cred = current_cred();
  802. if (IS_ERR(nd->intent.open.file))
  803. goto out;
  804. if (IS_ERR(dentry))
  805. goto out_err;
  806. nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
  807. nd->intent.open.file,
  808. open, cred);
  809. out:
  810. return nd->intent.open.file;
  811. out_err:
  812. release_open_intent(nd);
  813. nd->intent.open.file = (struct file *)dentry;
  814. goto out;
  815. }
  816. EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
  817. /**
  818. * nameidata_to_filp - convert a nameidata to an open filp.
  819. * @nd: pointer to nameidata
  820. * @flags: open flags
  821. *
  822. * Note that this function destroys the original nameidata
  823. */
  824. struct file *nameidata_to_filp(struct nameidata *nd)
  825. {
  826. const struct cred *cred = current_cred();
  827. struct file *filp;
  828. /* Pick up the filp from the open intent */
  829. filp = nd->intent.open.file;
  830. /* Has the filesystem initialised the file for us? */
  831. if (filp->f_path.dentry == NULL)
  832. filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
  833. NULL, cred);
  834. else
  835. path_put(&nd->path);
  836. return filp;
  837. }
  838. /*
  839. * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
  840. * error.
  841. */
  842. struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
  843. const struct cred *cred)
  844. {
  845. int error;
  846. struct file *f;
  847. validate_creds(cred);
  848. /*
  849. * We must always pass in a valid mount pointer. Historically
  850. * callers got away with not passing it, but we must enforce this at
  851. * the earliest possible point now to avoid strange problems deep in the
  852. * filesystem stack.
  853. */
  854. if (!mnt) {
  855. printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
  856. dump_stack();
  857. return ERR_PTR(-EINVAL);
  858. }
  859. error = -ENFILE;
  860. f = get_empty_filp();
  861. if (f == NULL) {
  862. dput(dentry);
  863. mntput(mnt);
  864. return ERR_PTR(error);
  865. }
  866. f->f_flags = flags;
  867. return __dentry_open(dentry, mnt, f, NULL, cred);
  868. }
  869. EXPORT_SYMBOL(dentry_open);
  870. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  871. {
  872. struct fdtable *fdt = files_fdtable(files);
  873. __FD_CLR(fd, fdt->open_fds);
  874. if (fd < files->next_fd)
  875. files->next_fd = fd;
  876. }
  877. void put_unused_fd(unsigned int fd)
  878. {
  879. struct files_struct *files = current->files;
  880. spin_lock(&files->file_lock);
  881. __put_unused_fd(files, fd);
  882. spin_unlock(&files->file_lock);
  883. }
  884. EXPORT_SYMBOL(put_unused_fd);
  885. /*
  886. * Install a file pointer in the fd array.
  887. *
  888. * The VFS is full of places where we drop the files lock between
  889. * setting the open_fds bitmap and installing the file in the file
  890. * array. At any such point, we are vulnerable to a dup2() race
  891. * installing a file in the array before us. We need to detect this and
  892. * fput() the struct file we are about to overwrite in this case.
  893. *
  894. * It should never happen - if we allow dup2() do it, _really_ bad things
  895. * will follow.
  896. */
  897. void fd_install(unsigned int fd, struct file *file)
  898. {
  899. struct files_struct *files = current->files;
  900. struct fdtable *fdt;
  901. spin_lock(&files->file_lock);
  902. fdt = files_fdtable(files);
  903. BUG_ON(fdt->fd[fd] != NULL);
  904. rcu_assign_pointer(fdt->fd[fd], file);
  905. spin_unlock(&files->file_lock);
  906. }
  907. EXPORT_SYMBOL(fd_install);
  908. long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
  909. {
  910. char *tmp = getname(filename);
  911. int fd = PTR_ERR(tmp);
  912. if (!IS_ERR(tmp)) {
  913. fd = get_unused_fd_flags(flags);
  914. if (fd >= 0) {
  915. struct file *f = do_filp_open(dfd, tmp, flags, mode, 0);
  916. if (IS_ERR(f)) {
  917. put_unused_fd(fd);
  918. fd = PTR_ERR(f);
  919. } else {
  920. fsnotify_open(f->f_path.dentry);
  921. fd_install(fd, f);
  922. }
  923. }
  924. putname(tmp);
  925. }
  926. return fd;
  927. }
  928. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
  929. {
  930. long ret;
  931. if (force_o_largefile())
  932. flags |= O_LARGEFILE;
  933. ret = do_sys_open(AT_FDCWD, filename, flags, mode);
  934. /* avoid REGPARM breakage on x86: */
  935. asmlinkage_protect(3, ret, filename, flags, mode);
  936. return ret;
  937. }
  938. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  939. int, mode)
  940. {
  941. long ret;
  942. if (force_o_largefile())
  943. flags |= O_LARGEFILE;
  944. ret = do_sys_open(dfd, filename, flags, mode);
  945. /* avoid REGPARM breakage on x86: */
  946. asmlinkage_protect(4, ret, dfd, filename, flags, mode);
  947. return ret;
  948. }
  949. #ifndef __alpha__
  950. /*
  951. * For backward compatibility? Maybe this should be moved
  952. * into arch/i386 instead?
  953. */
  954. SYSCALL_DEFINE2(creat, const char __user *, pathname, int, mode)
  955. {
  956. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  957. }
  958. #endif
  959. /*
  960. * "id" is the POSIX thread ID. We use the
  961. * files pointer for this..
  962. */
  963. int filp_close(struct file *filp, fl_owner_t id)
  964. {
  965. int retval = 0;
  966. if (!file_count(filp)) {
  967. printk(KERN_ERR "VFS: Close: file count is 0\n");
  968. return 0;
  969. }
  970. if (filp->f_op && filp->f_op->flush)
  971. retval = filp->f_op->flush(filp, id);
  972. dnotify_flush(filp, id);
  973. locks_remove_posix(filp, id);
  974. fput(filp);
  975. return retval;
  976. }
  977. EXPORT_SYMBOL(filp_close);
  978. /*
  979. * Careful here! We test whether the file pointer is NULL before
  980. * releasing the fd. This ensures that one clone task can't release
  981. * an fd while another clone is opening it.
  982. */
  983. SYSCALL_DEFINE1(close, unsigned int, fd)
  984. {
  985. struct file * filp;
  986. struct files_struct *files = current->files;
  987. struct fdtable *fdt;
  988. int retval;
  989. spin_lock(&files->file_lock);
  990. fdt = files_fdtable(files);
  991. if (fd >= fdt->max_fds)
  992. goto out_unlock;
  993. filp = fdt->fd[fd];
  994. if (!filp)
  995. goto out_unlock;
  996. rcu_assign_pointer(fdt->fd[fd], NULL);
  997. FD_CLR(fd, fdt->close_on_exec);
  998. __put_unused_fd(files, fd);
  999. spin_unlock(&files->file_lock);
  1000. retval = filp_close(filp, files);
  1001. /* can't restart close syscall because file table entry was cleared */
  1002. if (unlikely(retval == -ERESTARTSYS ||
  1003. retval == -ERESTARTNOINTR ||
  1004. retval == -ERESTARTNOHAND ||
  1005. retval == -ERESTART_RESTARTBLOCK))
  1006. retval = -EINTR;
  1007. return retval;
  1008. out_unlock:
  1009. spin_unlock(&files->file_lock);
  1010. return -EBADF;
  1011. }
  1012. EXPORT_SYMBOL(sys_close);
  1013. /*
  1014. * This routine simulates a hangup on the tty, to arrange that users
  1015. * are given clean terminals at login time.
  1016. */
  1017. SYSCALL_DEFINE0(vhangup)
  1018. {
  1019. if (capable(CAP_SYS_TTY_CONFIG)) {
  1020. tty_vhangup_self();
  1021. return 0;
  1022. }
  1023. return -EPERM;
  1024. }
  1025. /*
  1026. * Called when an inode is about to be open.
  1027. * We use this to disallow opening large files on 32bit systems if
  1028. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  1029. * on this flag in sys_open.
  1030. */
  1031. int generic_file_open(struct inode * inode, struct file * filp)
  1032. {
  1033. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  1034. return -EOVERFLOW;
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL(generic_file_open);
  1038. /*
  1039. * This is used by subsystems that don't want seekable
  1040. * file descriptors
  1041. */
  1042. int nonseekable_open(struct inode *inode, struct file *filp)
  1043. {
  1044. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1045. return 0;
  1046. }
  1047. EXPORT_SYMBOL(nonseekable_open);