open.c 28 KB

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