0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/export.h>
0010 #include <linux/time.h>
0011 #include <linux/mm.h>
0012 #include <linux/string.h>
0013 #include <linux/sched/signal.h>
0014 #include <linux/capability.h>
0015 #include <linux/fsnotify.h>
0016 #include <linux/fcntl.h>
0017 #include <linux/security.h>
0018 #include <linux/evm.h>
0019 #include <linux/ima.h>
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 static bool chown_ok(struct user_namespace *mnt_userns,
0034 const struct inode *inode, vfsuid_t ia_vfsuid)
0035 {
0036 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
0037 if (vfsuid_eq_kuid(vfsuid, current_fsuid()) &&
0038 vfsuid_eq(ia_vfsuid, vfsuid))
0039 return true;
0040 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
0041 return true;
0042 if (!vfsuid_valid(vfsuid) &&
0043 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
0044 return true;
0045 return false;
0046 }
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 static bool chgrp_ok(struct user_namespace *mnt_userns,
0061 const struct inode *inode, vfsgid_t ia_vfsgid)
0062 {
0063 vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
0064 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
0065 if (vfsuid_eq_kuid(vfsuid, current_fsuid())) {
0066 if (vfsgid_eq(ia_vfsgid, vfsgid))
0067 return true;
0068 if (vfsgid_in_group_p(ia_vfsgid))
0069 return true;
0070 }
0071 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
0072 return true;
0073 if (!vfsgid_valid(vfsgid) &&
0074 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
0075 return true;
0076 return false;
0077 }
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100 int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry,
0101 struct iattr *attr)
0102 {
0103 struct inode *inode = d_inode(dentry);
0104 unsigned int ia_valid = attr->ia_valid;
0105
0106
0107
0108
0109
0110 if (ia_valid & ATTR_SIZE) {
0111 int error = inode_newsize_ok(inode, attr->ia_size);
0112 if (error)
0113 return error;
0114 }
0115
0116
0117 if (ia_valid & ATTR_FORCE)
0118 goto kill_priv;
0119
0120
0121 if ((ia_valid & ATTR_UID) &&
0122 !chown_ok(mnt_userns, inode, attr->ia_vfsuid))
0123 return -EPERM;
0124
0125
0126 if ((ia_valid & ATTR_GID) &&
0127 !chgrp_ok(mnt_userns, inode, attr->ia_vfsgid))
0128 return -EPERM;
0129
0130
0131 if (ia_valid & ATTR_MODE) {
0132 vfsgid_t vfsgid;
0133
0134 if (!inode_owner_or_capable(mnt_userns, inode))
0135 return -EPERM;
0136
0137 if (ia_valid & ATTR_GID)
0138 vfsgid = attr->ia_vfsgid;
0139 else
0140 vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
0141
0142
0143 if (!vfsgid_in_group_p(vfsgid) &&
0144 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
0145 attr->ia_mode &= ~S_ISGID;
0146 }
0147
0148
0149 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
0150 if (!inode_owner_or_capable(mnt_userns, inode))
0151 return -EPERM;
0152 }
0153
0154 kill_priv:
0155
0156 if (ia_valid & ATTR_KILL_PRIV) {
0157 int error;
0158
0159 error = security_inode_killpriv(mnt_userns, dentry);
0160 if (error)
0161 return error;
0162 }
0163
0164 return 0;
0165 }
0166 EXPORT_SYMBOL(setattr_prepare);
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184 int inode_newsize_ok(const struct inode *inode, loff_t offset)
0185 {
0186 if (offset < 0)
0187 return -EINVAL;
0188 if (inode->i_size < offset) {
0189 unsigned long limit;
0190
0191 limit = rlimit(RLIMIT_FSIZE);
0192 if (limit != RLIM_INFINITY && offset > limit)
0193 goto out_sig;
0194 if (offset > inode->i_sb->s_maxbytes)
0195 goto out_big;
0196 } else {
0197
0198
0199
0200
0201
0202 if (IS_SWAPFILE(inode))
0203 return -ETXTBSY;
0204 }
0205
0206 return 0;
0207 out_sig:
0208 send_sig(SIGXFSZ, current, 0);
0209 out_big:
0210 return -EFBIG;
0211 }
0212 EXPORT_SYMBOL(inode_newsize_ok);
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239 void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
0240 const struct iattr *attr)
0241 {
0242 unsigned int ia_valid = attr->ia_valid;
0243
0244 i_uid_update(mnt_userns, attr, inode);
0245 i_gid_update(mnt_userns, attr, inode);
0246 if (ia_valid & ATTR_ATIME)
0247 inode->i_atime = attr->ia_atime;
0248 if (ia_valid & ATTR_MTIME)
0249 inode->i_mtime = attr->ia_mtime;
0250 if (ia_valid & ATTR_CTIME)
0251 inode->i_ctime = attr->ia_ctime;
0252 if (ia_valid & ATTR_MODE) {
0253 umode_t mode = attr->ia_mode;
0254 vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
0255 if (!vfsgid_in_group_p(vfsgid) &&
0256 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
0257 mode &= ~S_ISGID;
0258 inode->i_mode = mode;
0259 }
0260 }
0261 EXPORT_SYMBOL(setattr_copy);
0262
0263 int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
0264 unsigned int ia_valid)
0265 {
0266 int error;
0267
0268 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
0269 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
0270 return -EPERM;
0271 }
0272
0273
0274
0275
0276
0277 if (ia_valid & ATTR_TOUCH) {
0278 if (IS_IMMUTABLE(inode))
0279 return -EPERM;
0280
0281 if (!inode_owner_or_capable(mnt_userns, inode)) {
0282 error = inode_permission(mnt_userns, inode, MAY_WRITE);
0283 if (error)
0284 return error;
0285 }
0286 }
0287 return 0;
0288 }
0289 EXPORT_SYMBOL(may_setattr);
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318 int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
0319 struct iattr *attr, struct inode **delegated_inode)
0320 {
0321 struct inode *inode = dentry->d_inode;
0322 umode_t mode = inode->i_mode;
0323 int error;
0324 struct timespec64 now;
0325 unsigned int ia_valid = attr->ia_valid;
0326
0327 WARN_ON_ONCE(!inode_is_locked(inode));
0328
0329 error = may_setattr(mnt_userns, inode, ia_valid);
0330 if (error)
0331 return error;
0332
0333 if ((ia_valid & ATTR_MODE)) {
0334 umode_t amode = attr->ia_mode;
0335
0336 if (is_sxid(amode))
0337 inode->i_flags &= ~S_NOSEC;
0338 }
0339
0340 now = current_time(inode);
0341
0342 attr->ia_ctime = now;
0343 if (!(ia_valid & ATTR_ATIME_SET))
0344 attr->ia_atime = now;
0345 else
0346 attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
0347 if (!(ia_valid & ATTR_MTIME_SET))
0348 attr->ia_mtime = now;
0349 else
0350 attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
0351
0352 if (ia_valid & ATTR_KILL_PRIV) {
0353 error = security_inode_need_killpriv(dentry);
0354 if (error < 0)
0355 return error;
0356 if (error == 0)
0357 ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
0358 }
0359
0360
0361
0362
0363
0364
0365
0366
0367 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
0368 (ia_valid & ATTR_MODE))
0369 BUG();
0370
0371 if (ia_valid & ATTR_KILL_SUID) {
0372 if (mode & S_ISUID) {
0373 ia_valid = attr->ia_valid |= ATTR_MODE;
0374 attr->ia_mode = (inode->i_mode & ~S_ISUID);
0375 }
0376 }
0377 if (ia_valid & ATTR_KILL_SGID) {
0378 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
0379 if (!(ia_valid & ATTR_MODE)) {
0380 ia_valid = attr->ia_valid |= ATTR_MODE;
0381 attr->ia_mode = inode->i_mode;
0382 }
0383 attr->ia_mode &= ~S_ISGID;
0384 }
0385 }
0386 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
0387 return 0;
0388
0389
0390
0391
0392
0393 if (ia_valid & ATTR_UID &&
0394 !vfsuid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
0395 attr->ia_vfsuid))
0396 return -EOVERFLOW;
0397 if (ia_valid & ATTR_GID &&
0398 !vfsgid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
0399 attr->ia_vfsgid))
0400 return -EOVERFLOW;
0401
0402
0403
0404
0405 if (!(ia_valid & ATTR_UID) &&
0406 !vfsuid_valid(i_uid_into_vfsuid(mnt_userns, inode)))
0407 return -EOVERFLOW;
0408 if (!(ia_valid & ATTR_GID) &&
0409 !vfsgid_valid(i_gid_into_vfsgid(mnt_userns, inode)))
0410 return -EOVERFLOW;
0411
0412 error = security_inode_setattr(mnt_userns, dentry, attr);
0413 if (error)
0414 return error;
0415 error = try_break_deleg(inode, delegated_inode);
0416 if (error)
0417 return error;
0418
0419 if (inode->i_op->setattr)
0420 error = inode->i_op->setattr(mnt_userns, dentry, attr);
0421 else
0422 error = simple_setattr(mnt_userns, dentry, attr);
0423
0424 if (!error) {
0425 fsnotify_change(dentry, ia_valid);
0426 ima_inode_post_setattr(mnt_userns, dentry);
0427 evm_inode_post_setattr(dentry, ia_valid);
0428 }
0429
0430 return error;
0431 }
0432 EXPORT_SYMBOL(notify_change);