1818#include <linux/evm.h>
1919#include <linux/ima.h>
2020
21- static bool chown_ok (const struct inode * inode , kuid_t uid )
21+ /**
22+ * chown_ok - verify permissions to chown inode
23+ * @mnt_userns: user namespace of the mount @inode was found from
24+ * @inode: inode to check permissions on
25+ * @uid: uid to chown @inode to
26+ *
27+ * If the inode has been found through an idmapped mount the user namespace of
28+ * the vfsmount must be passed through @mnt_userns. This function will then
29+ * take care to map the inode according to @mnt_userns before checking
30+ * permissions. On non-idmapped mounts or if permission checking is to be
31+ * performed on the raw inode simply passs init_user_ns.
32+ */
33+ static bool chown_ok (struct user_namespace * mnt_userns ,
34+ const struct inode * inode ,
35+ kuid_t uid )
2236{
23- if ( uid_eq ( current_fsuid () , inode -> i_uid ) &&
24- uid_eq (uid , inode -> i_uid ))
37+ kuid_t kuid = i_uid_into_mnt ( mnt_userns , inode );
38+ if ( uid_eq ( current_fsuid (), kuid ) && uid_eq (uid , kuid ))
2539 return true;
26- if (capable_wrt_inode_uidgid (& init_user_ns , inode , CAP_CHOWN ))
40+ if (capable_wrt_inode_uidgid (mnt_userns , inode , CAP_CHOWN ))
2741 return true;
28- if (uid_eq (inode -> i_uid , INVALID_UID ) &&
42+ if (uid_eq (kuid , INVALID_UID ) &&
2943 ns_capable (inode -> i_sb -> s_user_ns , CAP_CHOWN ))
3044 return true;
3145 return false;
3246}
3347
34- static bool chgrp_ok (const struct inode * inode , kgid_t gid )
48+ /**
49+ * chgrp_ok - verify permissions to chgrp inode
50+ * @mnt_userns: user namespace of the mount @inode was found from
51+ * @inode: inode to check permissions on
52+ * @gid: gid to chown @inode to
53+ *
54+ * If the inode has been found through an idmapped mount the user namespace of
55+ * the vfsmount must be passed through @mnt_userns. This function will then
56+ * take care to map the inode according to @mnt_userns before checking
57+ * permissions. On non-idmapped mounts or if permission checking is to be
58+ * performed on the raw inode simply passs init_user_ns.
59+ */
60+ static bool chgrp_ok (struct user_namespace * mnt_userns ,
61+ const struct inode * inode , kgid_t gid )
3562{
36- if (uid_eq (current_fsuid (), inode -> i_uid ) &&
37- (in_group_p (gid ) || gid_eq (gid , inode -> i_gid )))
63+ kgid_t kgid = i_gid_into_mnt (mnt_userns , inode );
64+ if (uid_eq (current_fsuid (), i_uid_into_mnt (mnt_userns , inode )) &&
65+ (in_group_p (gid ) || gid_eq (gid , kgid )))
3866 return true;
39- if (capable_wrt_inode_uidgid (& init_user_ns , inode , CAP_CHOWN ))
67+ if (capable_wrt_inode_uidgid (mnt_userns , inode , CAP_CHOWN ))
4068 return true;
41- if (gid_eq (inode -> i_gid , INVALID_GID ) &&
69+ if (gid_eq (kgid , INVALID_GID ) &&
4270 ns_capable (inode -> i_sb -> s_user_ns , CAP_CHOWN ))
4371 return true;
4472 return false;
4573}
4674
4775/**
4876 * setattr_prepare - check if attribute changes to a dentry are allowed
77+ * @mnt_userns: user namespace of the mount the inode was found from
4978 * @dentry: dentry to check
5079 * @attr: attributes to change
5180 *
@@ -55,10 +84,17 @@ static bool chgrp_ok(const struct inode *inode, kgid_t gid)
5584 * SGID bit from mode if user is not allowed to set it. Also file capabilities
5685 * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
5786 *
87+ * If the inode has been found through an idmapped mount the user namespace of
88+ * the vfsmount must be passed through @mnt_userns. This function will then
89+ * take care to map the inode according to @mnt_userns before checking
90+ * permissions. On non-idmapped mounts or if permission checking is to be
91+ * performed on the raw inode simply passs init_user_ns.
92+ *
5893 * Should be called as the first thing in ->setattr implementations,
5994 * possibly after taking additional locks.
6095 */
61- int setattr_prepare (struct dentry * dentry , struct iattr * attr )
96+ int setattr_prepare (struct user_namespace * mnt_userns , struct dentry * dentry ,
97+ struct iattr * attr )
6298{
6399 struct inode * inode = d_inode (dentry );
64100 unsigned int ia_valid = attr -> ia_valid ;
@@ -78,27 +114,27 @@ int setattr_prepare(struct dentry *dentry, struct iattr *attr)
78114 goto kill_priv ;
79115
80116 /* Make sure a caller can chown. */
81- if ((ia_valid & ATTR_UID ) && !chown_ok (inode , attr -> ia_uid ))
117+ if ((ia_valid & ATTR_UID ) && !chown_ok (mnt_userns , inode , attr -> ia_uid ))
82118 return - EPERM ;
83119
84120 /* Make sure caller can chgrp. */
85- if ((ia_valid & ATTR_GID ) && !chgrp_ok (inode , attr -> ia_gid ))
121+ if ((ia_valid & ATTR_GID ) && !chgrp_ok (mnt_userns , inode , attr -> ia_gid ))
86122 return - EPERM ;
87123
88124 /* Make sure a caller can chmod. */
89125 if (ia_valid & ATTR_MODE ) {
90- if (!inode_owner_or_capable (& init_user_ns , inode ))
126+ if (!inode_owner_or_capable (mnt_userns , inode ))
91127 return - EPERM ;
92128 /* Also check the setgid bit! */
93- if (!in_group_p ((ia_valid & ATTR_GID ) ? attr -> ia_gid :
94- inode -> i_gid ) &&
95- !capable_wrt_inode_uidgid (& init_user_ns , inode , CAP_FSETID ))
129+ if (!in_group_p ((ia_valid & ATTR_GID ) ? attr -> ia_gid :
130+ i_gid_into_mnt ( mnt_userns , inode ) ) &&
131+ !capable_wrt_inode_uidgid (mnt_userns , inode , CAP_FSETID ))
96132 attr -> ia_mode &= ~S_ISGID ;
97133 }
98134
99135 /* Check for setting the inode time. */
100136 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET )) {
101- if (!inode_owner_or_capable (& init_user_ns , inode ))
137+ if (!inode_owner_or_capable (mnt_userns , inode ))
102138 return - EPERM ;
103139 }
104140
@@ -162,20 +198,33 @@ EXPORT_SYMBOL(inode_newsize_ok);
162198
163199/**
164200 * setattr_copy - copy simple metadata updates into the generic inode
201+ * @mnt_userns: user namespace of the mount the inode was found from
165202 * @inode: the inode to be updated
166203 * @attr: the new attributes
167204 *
168205 * setattr_copy must be called with i_mutex held.
169206 *
170207 * setattr_copy updates the inode's metadata with that specified
171- * in attr. Noticeably missing is inode size update, which is more complex
208+ * in attr on idmapped mounts. If file ownership is changed setattr_copy
209+ * doesn't map ia_uid and ia_gid. It will asssume the caller has already
210+ * provided the intended values. Necessary permission checks to determine
211+ * whether or not the S_ISGID property needs to be removed are performed with
212+ * the correct idmapped mount permission helpers.
213+ * Noticeably missing is inode size update, which is more complex
172214 * as it requires pagecache updates.
173215 *
216+ * If the inode has been found through an idmapped mount the user namespace of
217+ * the vfsmount must be passed through @mnt_userns. This function will then
218+ * take care to map the inode according to @mnt_userns before checking
219+ * permissions. On non-idmapped mounts or if permission checking is to be
220+ * performed on the raw inode simply passs init_user_ns.
221+ *
174222 * The inode is not marked as dirty after this operation. The rationale is
175223 * that for "simple" filesystems, the struct inode is the inode storage.
176224 * The caller is free to mark the inode dirty afterwards if needed.
177225 */
178- void setattr_copy (struct inode * inode , const struct iattr * attr )
226+ void setattr_copy (struct user_namespace * mnt_userns , struct inode * inode ,
227+ const struct iattr * attr )
179228{
180229 unsigned int ia_valid = attr -> ia_valid ;
181230
@@ -191,9 +240,9 @@ void setattr_copy(struct inode *inode, const struct iattr *attr)
191240 inode -> i_ctime = attr -> ia_ctime ;
192241 if (ia_valid & ATTR_MODE ) {
193242 umode_t mode = attr -> ia_mode ;
194-
195- if (!in_group_p (inode -> i_gid ) &&
196- !capable_wrt_inode_uidgid (& init_user_ns , inode , CAP_FSETID ))
243+ kgid_t kgid = i_gid_into_mnt ( mnt_userns , inode );
244+ if (!in_group_p (kgid ) &&
245+ !capable_wrt_inode_uidgid (mnt_userns , inode , CAP_FSETID ))
197246 mode &= ~S_ISGID ;
198247 inode -> i_mode = mode ;
199248 }
@@ -202,6 +251,7 @@ EXPORT_SYMBOL(setattr_copy);
202251
203252/**
204253 * notify_change - modify attributes of a filesytem object
254+ * @mnt_userns: user namespace of the mount the inode was found from
205255 * @dentry: object affected
206256 * @attr: new attributes
207257 * @delegated_inode: returns inode, if the inode is delegated
@@ -214,13 +264,23 @@ EXPORT_SYMBOL(setattr_copy);
214264 * retry. Because breaking a delegation may take a long time, the
215265 * caller should drop the i_mutex before doing so.
216266 *
267+ * If file ownership is changed notify_change() doesn't map ia_uid and
268+ * ia_gid. It will asssume the caller has already provided the intended values.
269+ *
217270 * Alternatively, a caller may pass NULL for delegated_inode. This may
218271 * be appropriate for callers that expect the underlying filesystem not
219272 * to be NFS exported. Also, passing NULL is fine for callers holding
220273 * the file open for write, as there can be no conflicting delegation in
221274 * that case.
275+ *
276+ * If the inode has been found through an idmapped mount the user namespace of
277+ * the vfsmount must be passed through @mnt_userns. This function will then
278+ * take care to map the inode according to @mnt_userns before checking
279+ * permissions. On non-idmapped mounts or if permission checking is to be
280+ * performed on the raw inode simply passs init_user_ns.
222281 */
223- int notify_change (struct dentry * dentry , struct iattr * attr , struct inode * * delegated_inode )
282+ int notify_change (struct user_namespace * mnt_userns , struct dentry * dentry ,
283+ struct iattr * attr , struct inode * * delegated_inode )
224284{
225285 struct inode * inode = dentry -> d_inode ;
226286 umode_t mode = inode -> i_mode ;
@@ -243,9 +303,8 @@ int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **de
243303 if (IS_IMMUTABLE (inode ))
244304 return - EPERM ;
245305
246- if (!inode_owner_or_capable (& init_user_ns , inode )) {
247- error = inode_permission (& init_user_ns , inode ,
248- MAY_WRITE );
306+ if (!inode_owner_or_capable (mnt_userns , inode )) {
307+ error = inode_permission (mnt_userns , inode , MAY_WRITE );
249308 if (error )
250309 return error ;
251310 }
@@ -321,9 +380,11 @@ int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **de
321380 /* Don't allow modifications of files with invalid uids or
322381 * gids unless those uids & gids are being made valid.
323382 */
324- if (!(ia_valid & ATTR_UID ) && !uid_valid (inode -> i_uid ))
383+ if (!(ia_valid & ATTR_UID ) &&
384+ !uid_valid (i_uid_into_mnt (mnt_userns , inode )))
325385 return - EOVERFLOW ;
326- if (!(ia_valid & ATTR_GID ) && !gid_valid (inode -> i_gid ))
386+ if (!(ia_valid & ATTR_GID ) &&
387+ !gid_valid (i_gid_into_mnt (mnt_userns , inode )))
327388 return - EOVERFLOW ;
328389
329390 error = security_inode_setattr (dentry , attr );
0 commit comments