attr.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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)) {
  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. now = current_fs_time(inode->i_sb);
  96. attr->ia_ctime = now;
  97. if (!(ia_valid & ATTR_ATIME_SET))
  98. attr->ia_atime = now;
  99. if (!(ia_valid & ATTR_MTIME_SET))
  100. attr->ia_mtime = now;
  101. if (ia_valid & ATTR_KILL_PRIV) {
  102. attr->ia_valid &= ~ATTR_KILL_PRIV;
  103. ia_valid &= ~ATTR_KILL_PRIV;
  104. error = security_inode_need_killpriv(dentry);
  105. if (error > 0)
  106. error = security_inode_killpriv(dentry);
  107. if (error)
  108. return error;
  109. }
  110. /*
  111. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  112. * that the function has the ability to reinterpret a mode change
  113. * that's due to these bits. This adds an implicit restriction that
  114. * no function will ever call notify_change with both ATTR_MODE and
  115. * ATTR_KILL_S*ID set.
  116. */
  117. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  118. (ia_valid & ATTR_MODE))
  119. BUG();
  120. if (ia_valid & ATTR_KILL_SUID) {
  121. if (mode & S_ISUID) {
  122. ia_valid = attr->ia_valid |= ATTR_MODE;
  123. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  124. }
  125. }
  126. if (ia_valid & ATTR_KILL_SGID) {
  127. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  128. if (!(ia_valid & ATTR_MODE)) {
  129. ia_valid = attr->ia_valid |= ATTR_MODE;
  130. attr->ia_mode = inode->i_mode;
  131. }
  132. attr->ia_mode &= ~S_ISGID;
  133. }
  134. }
  135. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  136. return 0;
  137. if (ia_valid & ATTR_SIZE)
  138. down_write(&dentry->d_inode->i_alloc_sem);
  139. if (inode->i_op && inode->i_op->setattr) {
  140. error = security_inode_setattr(dentry, attr);
  141. if (!error)
  142. error = inode->i_op->setattr(dentry, attr);
  143. } else {
  144. error = inode_change_ok(inode, attr);
  145. if (!error)
  146. error = security_inode_setattr(dentry, attr);
  147. if (!error) {
  148. if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  149. (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid))
  150. error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
  151. if (!error)
  152. error = inode_setattr(inode, attr);
  153. }
  154. }
  155. if (ia_valid & ATTR_SIZE)
  156. up_write(&dentry->d_inode->i_alloc_sem);
  157. if (!error)
  158. fsnotify_change(dentry, ia_valid);
  159. return error;
  160. }
  161. EXPORT_SYMBOL(notify_change);