When developing a dedicated LSM module, we need to operate on the file object within the LSM function, such as retrieving the path. However, in `security_file_alloc()`, the passed-in `filp` is only a valid pointer; the content of `filp` is completely uninitialized and entirely random, which confuses the LSM function. Therefore, it is necessary to call `security_file_alloc()` only after the main fields of the `filp` object have been initialized. This patch only moves the call to `security_file_alloc()` to the end of the `init_file()` function. Signed-off-by: Tianjia Zhang --- fs/file_table.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/fs/file_table.c b/fs/file_table.c index 81c72576e548..e66531a629aa 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -156,11 +156,6 @@ static int init_file(struct file *f, int flags, const struct cred *cred) int error; f->f_cred = get_cred(cred); - error = security_file_alloc(f); - if (unlikely(error)) { - put_cred(f->f_cred); - return error; - } spin_lock_init(&f->f_lock); /* @@ -202,6 +197,14 @@ static int init_file(struct file *f, int flags, const struct cred *cred) * They may be enabled later by fsnotify_open_perm_and_set_mode(). */ file_set_fsnotify_mode(f, FMODE_NONOTIFY_PERM); + + error = security_file_alloc(f); + if (unlikely(error)) { + mutex_destroy(&f->f_pos_lock); + put_cred(f->f_cred); + return error; + } + return 0; } -- 2.39.5 (Apple Git-154)