diff mbox series

LSM: add NULL check for kcalloc()

Message ID 20210712234434.GA29205@raspberrypi (mailing list archive)
State New, archived
Headers show
Series LSM: add NULL check for kcalloc() | expand

Commit Message

Austin Kim July 12, 2021, 11:44 p.m. UTC
From: Austin Kim <austin.kim@lge.com>

kcalloc() may return NULL when memory allocation fails.
So it is necessary to add NULL check after the call to kcalloc() is made.

Signed-off-by: Austin Kim <austin.kim@lge.com>
---
 security/security.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

James Morris July 14, 2021, 7:05 p.m. UTC | #1
On Tue, 13 Jul 2021, Austin Kim wrote:

> From: Austin Kim <austin.kim@lge.com>
> 
> kcalloc() may return NULL when memory allocation fails.
> So it is necessary to add NULL check after the call to kcalloc() is made.
> 
> Signed-off-by: Austin Kim <austin.kim@lge.com>
> ---
>  security/security.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/security/security.c b/security/security.c
> index 09533cbb7221..f885c9e9bc35 100644
> --- a/security/security.c
> +++ b/security/security.c
> @@ -321,6 +321,8 @@ static void __init ordered_lsm_init(void)
>  
>  	ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
>  				GFP_KERNEL);
> +	if (ordered_lsms)
> +		return;

Your logic is reversed here.

Should this also be a kernel panic?

>  
>  	if (chosen_lsm_order) {
>  		if (chosen_major_lsm) {
> -- 
> 2.20.1
>
Austin Kim July 14, 2021, 9:44 p.m. UTC | #2
2021년 7월 15일 (목) 오전 4:12, James Morris <jmorris@namei.org>님이 작성:
>
> On Tue, 13 Jul 2021, Austin Kim wrote:
>
> > From: Austin Kim <austin.kim@lge.com>
> >
> > kcalloc() may return NULL when memory allocation fails.
> > So it is necessary to add NULL check after the call to kcalloc() is made.
> >
> > Signed-off-by: Austin Kim <austin.kim@lge.com>
> > ---
> >  security/security.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/security/security.c b/security/security.c
> > index 09533cbb7221..f885c9e9bc35 100644
> > --- a/security/security.c
> > +++ b/security/security.c
> > @@ -321,6 +321,8 @@ static void __init ordered_lsm_init(void)
> >
> >       ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
> >                               GFP_KERNEL);
> > +     if (ordered_lsms)
> > +             return;
>
> Your logic is reversed here.

I feel very sorry for my terrible mistake.
'if (ordered_lsms)' should have been 'if (!ordered_lsms)'.

Let me resend patch(v2) soon.

Thanks,
Austin Kim

>
> Should this also be a kernel panic?
>
> >
> >       if (chosen_lsm_order) {
> >               if (chosen_major_lsm) {
> > --
> > 2.20.1
> >
>
> --
> James Morris
> <jmorris@namei.org>
>
Ken Goldman Aug. 6, 2021, 6:33 p.m. UTC | #3
On 7/14/2021 5:44 PM, Austin Kim wrote:
> 2021년 7월 15일 (목) 오전 4:12, James Morris <jmorris@namei.org>님이 작성:
>>
>> On Tue, 13 Jul 2021, Austin Kim wrote:
>>
>>> From: Austin Kim <austin.kim@lge.com>
>>>
>>> kcalloc() may return NULL when memory allocation fails.
>>> So it is necessary to add NULL check after the call to kcalloc() is made.
>>>
>>> Signed-off-by: Austin Kim <austin.kim@lge.com>
>>> ---
>>>   security/security.c | 2 ++
>>>   1 file changed, 2 insertions(+)
>>>
>>> diff --git a/security/security.c b/security/security.c
>>> index 09533cbb7221..f885c9e9bc35 100644
>>> --- a/security/security.c
>>> +++ b/security/security.c
>>> @@ -321,6 +321,8 @@ static void __init ordered_lsm_init(void)
>>>
>>>        ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
>>>                                GFP_KERNEL);
>>> +     if (ordered_lsms)
>>> +             return;
>>
>> Your logic is reversed here.
> 
> I feel very sorry for my terrible mistake.
> 'if (ordered_lsms)' should have been 'if (!ordered_lsms)'.
> 

I know it's a bit more typing, but

	if (ordered_lsms == NULL)

compiles down to the same binary and avoids there errors that
try to treat a pointer as a boolean.
diff mbox series

Patch

diff --git a/security/security.c b/security/security.c
index 09533cbb7221..f885c9e9bc35 100644
--- a/security/security.c
+++ b/security/security.c
@@ -321,6 +321,8 @@  static void __init ordered_lsm_init(void)
 
 	ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
 				GFP_KERNEL);
+	if (ordered_lsms)
+		return;
 
 	if (chosen_lsm_order) {
 		if (chosen_major_lsm) {