lsm: add backing_file LSM hooks

Stacked filesystems such as overlayfs do not currently provide the
necessary mechanisms for LSMs to properly enforce access controls on the
mmap() and mprotect() operations.  In order to resolve this gap, a LSM
security blob is being added to the backing_file struct and the following
new LSM hooks are being created:

 security_backing_file_alloc()
 security_backing_file_free()
 security_mmap_backing_file()

The first two hooks are to manage the lifecycle of the LSM security blob
in the backing_file struct, while the third provides a new mmap() access
control point for the underlying backing file.  It is also expected that
LSMs will likely want to update their security_file_mprotect() callback
to address issues with their mprotect() controls, but that does not
require a change to the security_file_mprotect() LSM hook.

There are a three other small changes to support these new LSM hooks:
* Pass the user file associated with a backing file down to
alloc_empty_backing_file() so it can be included in the
security_backing_file_alloc() hook.
* Add getter and setter functions for the backing_file struct LSM blob
as the backing_file struct remains private to fs/file_table.c.
* Constify the file struct field in the LSM common_audit_data struct to
better support LSMs that need to pass a const file struct pointer into
the common LSM audit code.

Thanks to Arnd Bergmann for identifying the missing EXPORT_SYMBOL_GPL()
and supplying a fixup.

Cc: stable@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org
Cc: linux-unionfs@vger.kernel.org
Cc: linux-erofs@lists.ozlabs.org
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Reviewed-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Paul Moore <paul@paul-moore.com>
This commit is contained in:
Paul Moore
2025-12-19 13:18:22 -05:00
parent 880bd496ec
commit 6af36aeb14
16 changed files with 206 additions and 17 deletions

View File

@@ -81,6 +81,7 @@ const struct lsm_id *lsm_idlist[MAX_LSM_COUNT];
struct lsm_blob_sizes blob_sizes;
struct kmem_cache *lsm_file_cache;
struct kmem_cache *lsm_backing_file_cache;
struct kmem_cache *lsm_inode_cache;
#define SECURITY_HOOK_ACTIVE_KEY(HOOK, IDX) security_hook_active_##HOOK##_##IDX
@@ -172,6 +173,30 @@ static int lsm_file_alloc(struct file *file)
return 0;
}
/**
* lsm_backing_file_alloc - allocate a composite backing file blob
* @backing_file: the backing file
*
* Allocate the backing file blob for all the modules.
*
* Returns 0, or -ENOMEM if memory can't be allocated.
*/
static int lsm_backing_file_alloc(struct file *backing_file)
{
void *blob;
if (!lsm_backing_file_cache) {
backing_file_set_security(backing_file, NULL);
return 0;
}
blob = kmem_cache_zalloc(lsm_backing_file_cache, GFP_KERNEL);
backing_file_set_security(backing_file, blob);
if (!blob)
return -ENOMEM;
return 0;
}
/**
* lsm_blob_alloc - allocate a composite blob
* @dest: the destination for the blob
@@ -2417,6 +2442,57 @@ void security_file_free(struct file *file)
}
}
/**
* security_backing_file_alloc() - Allocate and setup a backing file blob
* @backing_file: the backing file
* @user_file: the associated user visible file
*
* Allocate a backing file LSM blob and perform any necessary initialization of
* the LSM blob. There will be some operations where the LSM will not have
* access to @user_file after this point, so any important state associated
* with @user_file that is important to the LSM should be captured in the
* backing file's LSM blob.
*
* LSM's should avoid taking a reference to @user_file in this hook as it will
* result in problems later when the system attempts to drop/put the file
* references due to a circular dependency.
*
* Return: Return 0 if the hook is successful, negative values otherwise.
*/
int security_backing_file_alloc(struct file *backing_file,
const struct file *user_file)
{
int rc;
rc = lsm_backing_file_alloc(backing_file);
if (rc)
return rc;
rc = call_int_hook(backing_file_alloc, backing_file, user_file);
if (unlikely(rc))
security_backing_file_free(backing_file);
return rc;
}
/**
* security_backing_file_free() - Free a backing file blob
* @backing_file: the backing file
*
* Free any LSM state associate with a backing file's LSM blob, including the
* blob itself.
*/
void security_backing_file_free(struct file *backing_file)
{
void *blob = backing_file_security(backing_file);
call_void_hook(backing_file_free, backing_file);
if (blob) {
backing_file_set_security(backing_file, NULL);
kmem_cache_free(lsm_backing_file_cache, blob);
}
}
/**
* security_file_ioctl() - Check if an ioctl is allowed
* @file: associated file
@@ -2505,6 +2581,32 @@ int security_mmap_file(struct file *file, unsigned long prot,
flags);
}
/**
* security_mmap_backing_file - Check if mmap'ing a backing file is allowed
* @vma: the vm_area_struct for the mmap'd region
* @backing_file: the backing file being mmap'd
* @user_file: the user file being mmap'd
*
* Check permissions for a mmap operation on a stacked filesystem. This hook
* is called after the security_mmap_file() and is responsible for authorizing
* the mmap on @backing_file. It is important to note that the mmap operation
* on @user_file has already been authorized and the @vma->vm_file has been
* set to @backing_file.
*
* Return: Returns 0 if permission is granted.
*/
int security_mmap_backing_file(struct vm_area_struct *vma,
struct file *backing_file,
struct file *user_file)
{
/* recommended by the stackable filesystem devs */
if (WARN_ON_ONCE(!(backing_file->f_mode & FMODE_BACKING)))
return -EIO;
return call_int_hook(mmap_backing_file, vma, backing_file, user_file);
}
EXPORT_SYMBOL_GPL(security_mmap_backing_file);
/**
* security_mmap_addr() - Check if mmap'ing an address is allowed
* @addr: address