All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH][next] kexec_file: Use array_size() helper in memcpy()
@ 2020-06-16 18:20 ` Gustavo A. R. Silva
  0 siblings, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2020-06-16 18:20 UTC (permalink / raw)
  To: Eric Biederman; +Cc: kexec, linux-kernel, Kees Cook, Gustavo A. R. Silva

Use array_size() instead of the open-coded version in memcpy(). These
sorts of multiplication factors need to be wrapped in array_size().

Also, while there, use the preferred form for passing a size of a struct.
The alternative form where struct name is spelled out hurts readability
and introduces an opportunity for a bug when the pointer variable type is
changed but the corresponding sizeof that is passed as argument is not.

This issue was found with the help of Coccinelle and, audited and fixed
manually.

Addresses-KSPP-ID: https://github.com/KSPP/linux/issues/83
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 kernel/kexec_file.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index bb05fd52de85..2bbb001cd505 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -910,11 +910,11 @@ static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
 	 * The section headers in kexec_purgatory are read-only. In order to
 	 * have them modifiable make a temporary copy.
 	 */
-	sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
+	sechdrs = vzalloc(array_size(sizeof(*sechdrs), pi->ehdr->e_shnum));
 	if (!sechdrs)
 		return -ENOMEM;
 	memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
-	       pi->ehdr->e_shnum * sizeof(Elf_Shdr));
+	       array_size(sizeof(*sechdrs), pi->ehdr->e_shnum));
 	pi->sechdrs = sechdrs;
 
 	offset = 0;
-- 
2.27.0


_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

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

* [PATCH][next] kexec_file: Use array_size() helper in memcpy()
@ 2020-06-16 18:20 ` Gustavo A. R. Silva
  0 siblings, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2020-06-16 18:20 UTC (permalink / raw)
  To: Eric Biederman; +Cc: kexec, linux-kernel, Gustavo A. R. Silva, Kees Cook

Use array_size() instead of the open-coded version in memcpy(). These
sorts of multiplication factors need to be wrapped in array_size().

Also, while there, use the preferred form for passing a size of a struct.
The alternative form where struct name is spelled out hurts readability
and introduces an opportunity for a bug when the pointer variable type is
changed but the corresponding sizeof that is passed as argument is not.

This issue was found with the help of Coccinelle and, audited and fixed
manually.

Addresses-KSPP-ID: https://github.com/KSPP/linux/issues/83
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 kernel/kexec_file.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index bb05fd52de85..2bbb001cd505 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -910,11 +910,11 @@ static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
 	 * The section headers in kexec_purgatory are read-only. In order to
 	 * have them modifiable make a temporary copy.
 	 */
-	sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
+	sechdrs = vzalloc(array_size(sizeof(*sechdrs), pi->ehdr->e_shnum));
 	if (!sechdrs)
 		return -ENOMEM;
 	memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
-	       pi->ehdr->e_shnum * sizeof(Elf_Shdr));
+	       array_size(sizeof(*sechdrs), pi->ehdr->e_shnum));
 	pi->sechdrs = sechdrs;
 
 	offset = 0;
-- 
2.27.0


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

* Re: [PATCH][next] kexec_file: Use array_size() helper in memcpy()
  2020-06-16 18:20 ` Gustavo A. R. Silva
@ 2020-06-16 18:30   ` Kees Cook
  -1 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2020-06-16 18:30 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Gustavo A. R. Silva, kexec, Eric Biederman, linux-kernel

On Tue, Jun 16, 2020 at 01:20:41PM -0500, Gustavo A. R. Silva wrote:
> Use array_size() instead of the open-coded version in memcpy(). These
> sorts of multiplication factors need to be wrapped in array_size().
> 
> Also, while there, use the preferred form for passing a size of a struct.
> The alternative form where struct name is spelled out hurts readability
> and introduces an opportunity for a bug when the pointer variable type is
> changed but the corresponding sizeof that is passed as argument is not.
> 
> This issue was found with the help of Coccinelle and, audited and fixed
> manually.
> 
> Addresses-KSPP-ID: https://github.com/KSPP/linux/issues/83
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

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

* Re: [PATCH][next] kexec_file: Use array_size() helper in memcpy()
@ 2020-06-16 18:30   ` Kees Cook
  0 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2020-06-16 18:30 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Eric Biederman, kexec, linux-kernel, Gustavo A. R. Silva

On Tue, Jun 16, 2020 at 01:20:41PM -0500, Gustavo A. R. Silva wrote:
> Use array_size() instead of the open-coded version in memcpy(). These
> sorts of multiplication factors need to be wrapped in array_size().
> 
> Also, while there, use the preferred form for passing a size of a struct.
> The alternative form where struct name is spelled out hurts readability
> and introduces an opportunity for a bug when the pointer variable type is
> changed but the corresponding sizeof that is passed as argument is not.
> 
> This issue was found with the help of Coccinelle and, audited and fixed
> manually.
> 
> Addresses-KSPP-ID: https://github.com/KSPP/linux/issues/83
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

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

end of thread, other threads:[~2020-06-16 18:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-16 18:20 [PATCH][next] kexec_file: Use array_size() helper in memcpy() Gustavo A. R. Silva
2020-06-16 18:20 ` Gustavo A. R. Silva
2020-06-16 18:30 ` Kees Cook
2020-06-16 18:30   ` Kees Cook

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.