Re: [RFC 20/20] ima: Setup securityfs_ns for IMA namespace

James Bottomley
2021-12-01T17:56:56Z

On Tue, 2021-11-30 at 11:06 -0500, Stefan Berger wrote:
[...]
> +
> +/*
> + * Fix the ownership (uid/gid) of the dentry's that couldn't be set
> at the
> + * time of their creation because the user namespace wasn't
> configured, yet.
> + */
> +static void ima_fs_ns_fixup_uid_gid(struct ima_namespace *ns)
> +{
> + struct inode *inode;
> + size_t i;
> +
> + if (ns->file_ownership_fixes_done ||
> + ns->user_ns->uid_map.nr_extents == 0)
> + return;
> +
> + ns->file_ownership_fixes_done = true;
> + for (i = 0; i < IMAFS_DENTRY_LAST; i++) {
> + if (!ns->dentry[i])
> + continue;
> + inode = ns->dentry[i]->d_inode;
> + inode->i_uid = make_kuid(ns->user_ns, 0);
> + inode->i_gid = make_kgid(ns->user_ns, 0);
> + }
> +}
> +
> +/* Fix the permissions when a file is opened */
> +int ima_fs_ns_permission(struct user_namespace *mnt_userns, struct
> inode *inode,
> + int mask)
> +{
> + ima_fs_ns_fixup_uid_gid(get_current_ns());
> + return generic_permission(mnt_userns, inode, mask);
> +}
> +
> +const struct inode_operations ima_fs_ns_inode_operations = {
> + .lookup = simple_lookup,
> + .permission = ima_fs_ns_permission,
> +};
> +

In theory this uid/gid shifting should have already been done for you
and all of the above code should be unnecessary. What is supposed to
happen is that the mount of securityfs_ns in the new user namespace
should pick up a superblock s_user_ns for that new user namespace. Now
inode_alloc() uses i_uid_write(inode, 0) which maps back through the
s_user_ns to obtain the owner of the user namespace.

What can happen is that if you do the inode allocation before (or even
without) writing to the uid_map file, it maps back through an empty map
and ends up with -1 for i_uid ... is this what you're seeing?

James