attr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * linux/fs/attr.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * changes by Thomas Schoebel-Theuer
  6. */
  7. #include <linux/module.h>
  8. #include <linux/time.h>
  9. #include <linux/mm.h>
  10. #include <linux/string.h>
  11. #include <linux/capability.h>
  12. #include <linux/fsnotify.h>
  13. #include <linux/fcntl.h>
  14. #include <linux/quotaops.h>
  15. #include <linux/security.h>
  16. /* Taken over from the old code... */
  17. /* POSIX UID/GID verification for setting inode attributes. */
  18. int inode_change_ok(struct inode *inode, struct iattr *attr)
  19. {
  20. int retval = -EPERM;
  21. unsigned int ia_valid = attr->ia_valid;
  22. /* If force is set do it anyway. */
  23. if (ia_valid & ATTR_FORCE)
  24. goto fine;
  25. /* Make sure a caller can chown. */
  26. if ((ia_valid & ATTR_UID) &&
  27. (current_fsuid() != inode->i_uid ||
  28. attr->ia_uid != inode->i_uid) && !capable(CAP_CHOWN))
  29. goto error;
  30. /* Make sure caller can chgrp. */
  31. if ((ia_valid & ATTR_GID) &&
  32. (current_fsuid() != inode->i_uid ||
  33. (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid)) &&
  34. !capable(CAP_CHOWN))
  35. goto error;
  36. /* Make sure a caller can chmod. */
  37. if (ia_valid & ATTR_MODE) {
  38. if (!is_owner_or_cap(inode))
  39. goto error;
  40. /* Also check the setgid bit! */
  41. if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
  42. inode->i_gid) && !capable(CAP_FSETID))
  43. attr->ia_mode &= ~S_ISGID;
  44. }
  45. /* Check for setting the inode time. */
  46. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
  47. if (!is_owner_or_cap(inode))
  48. goto error;
  49. }
  50. fine:
  51. retval = 0;
  52. error:
  53. return retval;
  54. }
  55. EXPORT_SYMBOL(inode_change_ok);
  56. int inode_setattr(struct inode * inode, struct iattr * attr)
  57. {
  58. unsigned int ia_valid = attr->ia_valid;
  59. if (ia_valid & ATTR_SIZE &&
  60. attr->ia_size != i_size_read(inode)) {
  61. int error = vmtruncate(inode, attr->ia_size);
  62. if (error)
  63. return error;
  64. }
  65. if (ia_valid & ATTR_UID)
  66. inode->i_uid = attr->ia_uid;
  67. if (ia_valid & ATTR_GID)
  68. inode->i_gid = attr->ia_gid;
  69. if (ia_valid & ATTR_ATIME)
  70. inode->i_atime = timespec_trunc(attr->ia_atime,
  71. inode->i_sb->s_time_gran);
  72. if (ia_valid & ATTR_MTIME)
  73. inode->i_mtime = timespec_trunc(attr->ia_mtime,
  74. inode->i_sb->s_time_gran);
  75. if (ia_valid & ATTR_CTIME)
  76. inode->i_ctime = timespec_trunc(attr->ia_ctime,
  77. inode->i_sb->s_time_gran);
  78. if (ia_valid & ATTR_MODE) {
  79. umode_t mode = attr->ia_mode;
  80. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  81. mode &= ~S_ISGID;
  82. inode->i_mode = mode;
  83. }
  84. mark_inode_dirty(inode);
  85. return 0;
  86. }
  87. EXPORT_SYMBOL(inode_setattr);
  88. int notify_change(struct dentry * dentry, struct iattr * attr)
  89. {
  90. struct inode *inode = dentry->d_inode;
  91. mode_t mode = inode->i_mode;
  92. int error;
  93. struct timespec now;
  94. unsigned int ia_valid = attr->ia_valid;
  95. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
  96. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  97. return -EPERM;
  98. }
  99. now = current_fs_time(inode->i_sb);
  100. attr->ia_ctime = now;
  101. if (!(ia_valid & ATTR_ATIME_SET))
  102. attr->ia_atime = now;
  103. if (!(ia_valid & ATTR_MTIME_SET))
  104. attr->ia_mtime = now;
  105. if (ia_valid & ATTR_KILL_PRIV) {
  106. attr->ia_valid &= ~ATTR_KILL_PRIV;
  107. ia_valid &= ~ATTR_KILL_PRIV;
  108. error = security_inode_need_killpriv(dentry);
  109. if (error > 0)
  110. error = security_inode_killpriv(dentry);
  111. if (error)
  112. return error;
  113. }
  114. /*
  115. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  116. * that the function has the ability to reinterpret a mode change
  117. * that's due to these bits. This adds an implicit restriction that
  118. * no function will ever call notify_change with both ATTR_MODE and
  119. * ATTR_KILL_S*ID set.
  120. */
  121. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  122. (ia_valid & ATTR_MODE))
  123. BUG();
  124. if (ia_valid & ATTR_KILL_SUID) {
  125. if (mode & S_ISUID) {
  126. ia_valid = attr->ia_valid |= ATTR_MODE;
  127. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  128. }
  129. }
  130. if (ia_valid & ATTR_KILL_SGID) {
  131. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  132. if (!(ia_valid & ATTR_MODE)) {
  133. ia_valid = attr->ia_valid |= ATTR_MODE;
  134. attr->ia_mode = inode->i_mode;
  135. }
  136. attr->ia_mode &= ~S_ISGID;
  137. }
  138. }
  139. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  140. return 0;
  141. error = security_inode_setattr(dentry, attr);
  142. if (error)
  143. return error;
  144. if (ia_valid & ATTR_SIZE)
  145. down_write(&dentry->d_inode->i_alloc_sem);
  146. if (inode->i_op && inode->i_op->setattr) {
  147. error = inode->i_op->setattr(dentry, attr);
  148. } else {
  149. error = inode_change_ok(inode, attr);
  150. if (!error) {
  151. if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  152. (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid))
  153. error = vfs_dq_transfer(inode, attr) ?
  154. -EDQUOT : 0;
  155. if (!error)
  156. error = inode_setattr(inode, attr);
  157. }
  158. }
  159. if (ia_valid & ATTR_SIZE)
  160. up_write(&dentry->d_inode->i_alloc_sem);
  161. if (!error)
  162. fsnotify_change(dentry, ia_valid);
  163. return error;
  164. }
  165. EXPORT_SYMBOL(notify_change);