LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] LSM: add NULL check for kcalloc()
@ 2021-07-12 23:44 Austin Kim
  2021-07-14 19:05 ` James Morris
  0 siblings, 1 reply; 4+ messages in thread
From: Austin Kim @ 2021-07-12 23:44 UTC (permalink / raw)
  To: jmorris, serge, keescook
  Cc: linux-security-module, linux-kernel, austin.kim, kernel-team,
	austindh.kim

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;
 
 	if (chosen_lsm_order) {
 		if (chosen_major_lsm) {
-- 
2.20.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] LSM: add NULL check for kcalloc()
  2021-07-12 23:44 [PATCH] LSM: add NULL check for kcalloc() Austin Kim
@ 2021-07-14 19:05 ` James Morris
  2021-07-14 21:44   ` Austin Kim
  0 siblings, 1 reply; 4+ messages in thread
From: James Morris @ 2021-07-14 19:05 UTC (permalink / raw)
  To: Austin Kim
  Cc: serge, keescook, linux-security-module, linux-kernel, austin.kim,
	kernel-team

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
> 

-- 
James Morris
<jmorris@namei.org>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] LSM: add NULL check for kcalloc()
  2021-07-14 19:05 ` James Morris
@ 2021-07-14 21:44   ` Austin Kim
  2021-08-06 18:33     ` Ken Goldman
  0 siblings, 1 reply; 4+ messages in thread
From: Austin Kim @ 2021-07-14 21:44 UTC (permalink / raw)
  To: James Morris
  Cc: Serge E. Hallyn, Kees Cook, linux-security-module,
	Linux Kernel Mailing List, 김동현,
	kernel-team

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>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] LSM: add NULL check for kcalloc()
  2021-07-14 21:44   ` Austin Kim
@ 2021-08-06 18:33     ` Ken Goldman
  0 siblings, 0 replies; 4+ messages in thread
From: Ken Goldman @ 2021-08-06 18:33 UTC (permalink / raw)
  Cc: linux-security-module, Linux Kernel Mailing List, kernel-team

[-- Attachment #1: Type: text/plain, Size: 1257 bytes --]

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.



[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4490 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-08-06 18:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-12 23:44 [PATCH] LSM: add NULL check for kcalloc() Austin Kim
2021-07-14 19:05 ` James Morris
2021-07-14 21:44   ` Austin Kim
2021-08-06 18:33     ` Ken Goldman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).