Skip to content

Commit 8f46ff5

Browse files
robertosassupcmoore
authored andcommitted
security: Introduce file_post_open hook
In preparation to move IMA and EVM to the LSM infrastructure, introduce the file_post_open hook. Also, export security_file_post_open() for NFS. Based on policy, IMA calculates the digest of the file content and extends the TPM with the digest, verifies the file's integrity based on the digest, and/or includes the file digest in the audit log. LSMs could similarly take action depending on the file content and the access mask requested with open(). The new hook returns a value and can cause the open to be aborted. Signed-off-by: Roberto Sassu <[email protected]> Reviewed-by: Stefan Berger <[email protected]> Acked-by: Casey Schaufler <[email protected]> Reviewed-by: Mimi Zohar <[email protected]> Acked-by: Christian Brauner <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent dae52cb commit 8f46ff5

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

fs/namei.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3639,6 +3639,8 @@ static int do_open(struct nameidata *nd,
36393639
error = may_open(idmap, &nd->path, acc_mode, open_flag);
36403640
if (!error && !(file->f_mode & FMODE_OPENED))
36413641
error = vfs_open(&nd->path, file);
3642+
if (!error)
3643+
error = security_file_post_open(file, op->acc_mode);
36423644
if (!error)
36433645
error = ima_file_check(file, op->acc_mode);
36443646
if (!error && do_truncate)

fs/nfsd/vfs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,12 @@ __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
877877
goto out;
878878
}
879879

880+
host_err = security_file_post_open(file, may_flags);
881+
if (host_err) {
882+
fput(file);
883+
goto out;
884+
}
885+
880886
host_err = ima_file_check(file, may_flags);
881887
if (host_err) {
882888
fput(file);

include/linux/lsm_hook_defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ LSM_HOOK(int, 0, file_send_sigiotask, struct task_struct *tsk,
191191
struct fown_struct *fown, int sig)
192192
LSM_HOOK(int, 0, file_receive, struct file *file)
193193
LSM_HOOK(int, 0, file_open, struct file *file)
194+
LSM_HOOK(int, 0, file_post_open, struct file *file, int mask)
194195
LSM_HOOK(int, 0, file_truncate, struct file *file)
195196
LSM_HOOK(int, 0, task_alloc, struct task_struct *task,
196197
unsigned long clone_flags)

include/linux/security.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ int security_file_send_sigiotask(struct task_struct *tsk,
411411
struct fown_struct *fown, int sig);
412412
int security_file_receive(struct file *file);
413413
int security_file_open(struct file *file);
414+
int security_file_post_open(struct file *file, int mask);
414415
int security_file_truncate(struct file *file);
415416
int security_task_alloc(struct task_struct *task, unsigned long clone_flags);
416417
void security_task_free(struct task_struct *task);
@@ -1074,6 +1075,11 @@ static inline int security_file_open(struct file *file)
10741075
return 0;
10751076
}
10761077

1078+
static inline int security_file_post_open(struct file *file, int mask)
1079+
{
1080+
return 0;
1081+
}
1082+
10771083
static inline int security_file_truncate(struct file *file)
10781084
{
10791085
return 0;

security/security.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2966,6 +2966,23 @@ int security_file_open(struct file *file)
29662966
return fsnotify_open_perm(file);
29672967
}
29682968

2969+
/**
2970+
* security_file_post_open() - Evaluate a file after it has been opened
2971+
* @file: the file
2972+
* @mask: access mask
2973+
*
2974+
* Evaluate an opened file and the access mask requested with open(). The hook
2975+
* is useful for LSMs that require the file content to be available in order to
2976+
* make decisions.
2977+
*
2978+
* Return: Returns 0 if permission is granted.
2979+
*/
2980+
int security_file_post_open(struct file *file, int mask)
2981+
{
2982+
return call_int_hook(file_post_open, 0, file, mask);
2983+
}
2984+
EXPORT_SYMBOL_GPL(security_file_post_open);
2985+
29692986
/**
29702987
* security_file_truncate() - Check if truncating a file is allowed
29712988
* @file: file

0 commit comments

Comments
 (0)