From: Paul Moore <paul@paul-moore.com>
To: Cai Xinchen <caixinchen1@huawei.com>, <jmorris@namei.org>,
<serge@hallyn.com>, <stephen.smalley.work@gmail.com>,
<omosnace@redhat.com>, <amir73il@gmail.com>, <brauner@kernel.org>
Cc: <linux-security-module@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <selinux@vger.kernel.org>,
<caixinchen1@huawei.com>, <lujialin4@huawei.com>
Subject: Re: [PATCH 2/2] security: Fix call security_backing_file_free second time
Date: Thu, 02 Jul 2026 17:09:18 -0400 [thread overview]
Message-ID: <aa30283e5827cdff22a62421c8d8e088@paul-moore.com> (raw)
In-Reply-To: <20260626011720.1144213-3-caixinchen1@huawei.com>
On Jun 25, 2026 Cai Xinchen <caixinchen1@huawei.com> wrote:
>
> I found the following path:
>
> alloc_empty_backing-file
> init_file(&ff->file, xxx)
> -> file_ref_init(&f->f_ref, 1); // only 1
> error = init_backing_file
> -> security_backing_file_alloc
> -> rc = call_int_hook(backing_file_alloc, ...)
The good news is that as you mentioned, only SELinux defines a
backing_file_alloc hook and it always returns success/0.
> if (unlikely(rc))
> security_backing_file_free(backing_file); // first call
> if (unlikely(error)) {
> fput(&ff->file);
> -> if (unlikely(file_ref_put(&file->f_ref))) // zero
> __fput_deferred(file);
> -> ____fput -> __fput -> file_free(file);
> -> backing_file_free(backing_file(f));
> -> security_backing_file_free(&ff->file); // second call
>
> Currently, only SELinux has the lsm backing_file_alloc hook, and the
> backing_file_free hook is not set. When security_backing_file_free is
> called for the first time, the blobs pointer is set to NULL. Therefore,
> double free will not occur in the code.
>
> Fixes: 6af36aeb147a ("lsm: add backing_file LSM hooks")
> Signed-off-by: Cai Xinchen <caixinchen1@huawei.com>
> ---
> security/security.c | 5 +----
> 1 file changed, 1 insertion(+), 4 deletions(-)
>
> diff --git a/security/security.c b/security/security.c
> index 71aea8fdf014..595d3c73253e 100644
> --- a/security/security.c
> +++ b/security/security.c
> @@ -2468,11 +2468,8 @@ int security_backing_file_alloc(struct file *backing_file,
> 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;
> + return call_int_hook(backing_file_alloc, backing_file, user_file);
> }
I think the better option would be to move the
call_void_hook(backing_file_free, ...) call in security_backing_file_free()
into the if-statment true block before we set the backing file's LSM blob
pointer to NULL and free the LSM blob.
--
paul-moore.com
prev parent reply other threads:[~2026-07-02 21:09 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-26 1:17 [PATCH -next 0/2] Fix call security_backing_file_free second time Cai Xinchen
2026-06-26 1:17 ` [PATCH -next 1/2] security: Some cleanup code Cai Xinchen
2026-07-02 21:09 ` [PATCH " Paul Moore
2026-06-26 1:17 ` [PATCH -next 2/2] security: Fix call security_backing_file_free second time Cai Xinchen
2026-07-02 21:09 ` Paul Moore [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aa30283e5827cdff22a62421c8d8e088@paul-moore.com \
--to=paul@paul-moore.com \
--cc=amir73il@gmail.com \
--cc=brauner@kernel.org \
--cc=caixinchen1@huawei.com \
--cc=jmorris@namei.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=lujialin4@huawei.com \
--cc=omosnace@redhat.com \
--cc=selinux@vger.kernel.org \
--cc=serge@hallyn.com \
--cc=stephen.smalley.work@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox