diff mbox series

keys: Use struct_size and size_add helper with alloc

Message ID 20220519024702.215223-1-guozihua@huawei.com (mailing list archive)
State New, archived
Headers show
Series keys: Use struct_size and size_add helper with alloc | expand

Commit Message

Guozihua (Scott) May 19, 2022, 2:47 a.m. UTC
Use struct_size helper for calculating size of flexible struct to avoid
potential issues and improve readibility. Use size_add to calculate
addition of size to prevent potential issues.

Reference: https://lore.kernel.org/all/CAHk-=wiGWjxs7EVUpccZEi6esvjpHJdgHQ=vtUeJ5crL62hx9A@mail.gmail.com/

Note: HASH_SIZE here is a SHA256_DIGEST_SIZE whoes value is 32, so
adding 1 should be fine here.

Signed-off-by: GUO Zihua <guozihua@huawei.com>
---
 security/keys/encrypted-keys/encrypted.c | 7 +++++--
 security/keys/user_defined.c             | 2 +-
 2 files changed, 6 insertions(+), 3 deletions(-)

Comments

Jarkko Sakkinen May 19, 2022, 8:49 p.m. UTC | #1
On Thu, May 19, 2022 at 10:47:02AM +0800, GUO Zihua wrote:
> Use struct_size helper for calculating size of flexible struct to avoid
> potential issues and improve readibility. Use size_add to calculate
> addition of size to prevent potential issues.

What issues? Please explicitly state the issues.

BR, Jarkko
Guozihua (Scott) May 20, 2022, 7:35 a.m. UTC | #2
On 2022/5/20 4:49, Jarkko Sakkinen wrote:
> On Thu, May 19, 2022 at 10:47:02AM +0800, GUO Zihua wrote:
>> Use struct_size helper for calculating size of flexible struct to avoid
>> potential issues and improve readibility. Use size_add to calculate
>> addition of size to prevent potential issues.
> 
> What issues? Please explicitly state the issues.
> 
> BR, Jarkko
> .

Hi Jarkko,

This is a clean-up/optimization patch mostly focuses on readability and 
no actual issue is observed at the moment.
Jarkko Sakkinen May 23, 2022, 7:58 p.m. UTC | #3
On Fri, May 20, 2022 at 03:35:09PM +0800, Guozihua (Scott) wrote:
> On 2022/5/20 4:49, Jarkko Sakkinen wrote:
> > On Thu, May 19, 2022 at 10:47:02AM +0800, GUO Zihua wrote:
> > > Use struct_size helper for calculating size of flexible struct to avoid
> > > potential issues and improve readibility. Use size_add to calculate
> > > addition of size to prevent potential issues.
> > 
> > What issues? Please explicitly state the issues.
> > 
> > BR, Jarkko
> > .
> 
> Hi Jarkko,
> 
> This is a clean-up/optimization patch mostly focuses on readability and no
> actual issue is observed at the moment.

But the commit message is talking about potential issues.

BR, Jarkko
Guozihua (Scott) May 24, 2022, 1:58 a.m. UTC | #4
On 2022/5/24 3:58, Jarkko Sakkinen wrote:
> On Fri, May 20, 2022 at 03:35:09PM +0800, Guozihua (Scott) wrote:
>> On 2022/5/20 4:49, Jarkko Sakkinen wrote:
>>> On Thu, May 19, 2022 at 10:47:02AM +0800, GUO Zihua wrote:
>>>> Use struct_size helper for calculating size of flexible struct to avoid
>>>> potential issues and improve readibility. Use size_add to calculate
>>>> addition of size to prevent potential issues.
>>>
>>> What issues? Please explicitly state the issues.
>>>
>>> BR, Jarkko
>>> .
>>
>> Hi Jarkko,
>>
>> This is a clean-up/optimization patch mostly focuses on readability and no
>> actual issue is observed at the moment.
> 
> But the commit message is talking about potential issues.
> 
> BR, Jarkko
> .
Thanks Jarkko.

Yes, and it should be considered wrong. I've sent a v2 patch addressing 
this.
diff mbox series

Patch

diff --git a/security/keys/encrypted-keys/encrypted.c b/security/keys/encrypted-keys/encrypted.c
index e05cfc2e49ae..37349580e855 100644
--- a/security/keys/encrypted-keys/encrypted.c
+++ b/security/keys/encrypted-keys/encrypted.c
@@ -613,6 +613,7 @@  static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
 	long dlen;
 	int i;
 	int ret;
+	size_t epayload_datalen = 0;
 
 	ret = kstrtol(datalen, 10, &dlen);
 	if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
@@ -667,8 +668,10 @@  static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
 	if (ret < 0)
 		return ERR_PTR(ret);
 
-	epayload = kzalloc(sizeof(*epayload) + payload_datalen +
-			   datablob_len + HASH_SIZE + 1, GFP_KERNEL);
+	epayload_datalen = size_add(payload_datalen, datablob_len);
+	epayload_datalen = size_add(epayload_datalen, HASH_SIZE + 1);
+	epayload = kzalloc(struct_size(epayload, payload_data,
+				epayload_datalen), GFP_KERNEL);
 	if (!epayload)
 		return ERR_PTR(-ENOMEM);
 
diff --git a/security/keys/user_defined.c b/security/keys/user_defined.c
index 749e2a4dcb13..334fed36e9f3 100644
--- a/security/keys/user_defined.c
+++ b/security/keys/user_defined.c
@@ -64,7 +64,7 @@  int user_preparse(struct key_preparsed_payload *prep)
 	if (datalen <= 0 || datalen > 32767 || !prep->data)
 		return -EINVAL;
 
-	upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
+	upayload = kmalloc(struct_size(upayload, data, datalen), GFP_KERNEL);
 	if (!upayload)
 		return -ENOMEM;