All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fs/coda: Do not use partially allocated struct
@ 2022-05-20 16:59 Kees Cook
  2022-05-20 17:11 ` Jan Harkes
  0 siblings, 1 reply; 2+ messages in thread
From: Kees Cook @ 2022-05-20 16:59 UTC (permalink / raw)
  To: Jan Harkes; +Cc: Kees Cook, coda, codalist, linux-kernel, linux-hardening

GCC 12 does not like seeing a partially allocated structure being used,
especially when a pointer is being passed out of function scope. Since
only the struct coda_in_hdr member of union inputArgs is being allocated
and used, just replace union inputArgs with struct coda_in_hdr.

../fs/coda/upcall.c: In function 'coda_upcall':
../fs/coda/upcall.c:801:22: warning: array subscript 'union inputArgs[0]' is partly outside array bounds of 'unsigned char[20]' [-Warray-bounds]
  801 |         sig_inputArgs->ih.opcode = CODA_SIGNAL;
      |                      ^~
In file included from ../include/linux/fs.h:45,
                 from ../include/linux/huge_mm.h:8,
                 from ../include/linux/mm.h:700,
                 from ../fs/coda/upcall.c:22:
In function 'kvmalloc',
    inlined from 'kvzalloc' at ../include/linux/slab.h:758:9,
    inlined from 'coda_upcall' at ../fs/coda/upcall.c:794:18:
../include/linux/slab.h:750:16: note: object of size 20 allocated by 'kvmalloc_node'
  750 |         return kvmalloc_node(size, flags, NUMA_NO_NODE);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../fs/coda/upcall.c: In function 'coda_upcall':
../fs/coda/upcall.c:802:22: warning: array subscript 'union inputArgs[0]' is partly outside array bounds of 'unsigned char[20]' [-Warray-bounds]
  802 |         sig_inputArgs->ih.unique = req->uc_unique;
      |                      ^~
In function 'kvmalloc',
    inlined from 'kvzalloc' at ../include/linux/slab.h:758:9,
    inlined from 'coda_upcall' at ../fs/coda/upcall.c:794:18:
../include/linux/slab.h:750:16: note: object of size 20 allocated by 'kvmalloc_node'
  750 |         return kvmalloc_node(size, flags, NUMA_NO_NODE);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Cc: Jan Harkes <jaharkes@cs.cmu.edu>
Cc: coda@cs.cmu.edu
Cc: codalist@coda.cs.cmu.edu
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 fs/coda/upcall.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/fs/coda/upcall.c b/fs/coda/upcall.c
index 59f6cfd06f96..21e4f5f446b2 100644
--- a/fs/coda/upcall.c
+++ b/fs/coda/upcall.c
@@ -711,7 +711,7 @@ static int coda_upcall(struct venus_comm *vcp,
 		       union inputArgs *buffer)
 {
 	union outputArgs *out;
-	union inputArgs *sig_inputArgs;
+	struct coda_in_hdr *ih;
 	struct upc_req *req = NULL, *sig_req;
 	int error;
 
@@ -791,22 +791,22 @@ static int coda_upcall(struct venus_comm *vcp,
 	sig_req = kmalloc(sizeof(struct upc_req), GFP_KERNEL);
 	if (!sig_req) goto exit;
 
-	sig_inputArgs = kvzalloc(sizeof(struct coda_in_hdr), GFP_KERNEL);
-	if (!sig_inputArgs) {
+	ih = kvzalloc(sizeof(*ih), GFP_KERNEL);
+	if (!ih) {
 		kfree(sig_req);
 		goto exit;
 	}
 
 	error = -EINTR;
-	sig_inputArgs->ih.opcode = CODA_SIGNAL;
-	sig_inputArgs->ih.unique = req->uc_unique;
+	ih->opcode = CODA_SIGNAL;
+	ih->unique = req->uc_unique;
 
 	sig_req->uc_flags = CODA_REQ_ASYNC;
-	sig_req->uc_opcode = sig_inputArgs->ih.opcode;
-	sig_req->uc_unique = sig_inputArgs->ih.unique;
-	sig_req->uc_data = (void *)sig_inputArgs;
-	sig_req->uc_inSize = sizeof(struct coda_in_hdr);
-	sig_req->uc_outSize = sizeof(struct coda_in_hdr);
+	sig_req->uc_opcode = ih->opcode;
+	sig_req->uc_unique = ih->unique;
+	sig_req->uc_data = (void *)ih;
+	sig_req->uc_inSize = sizeof(*ih);
+	sig_req->uc_outSize = sizeof(*ih);
 
 	/* insert at head of queue! */
 	list_add(&(sig_req->uc_chain), &vcp->vc_pending);
-- 
2.32.0


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

* Re: [PATCH] fs/coda: Do not use partially allocated struct
  2022-05-20 16:59 [PATCH] fs/coda: Do not use partially allocated struct Kees Cook
@ 2022-05-20 17:11 ` Jan Harkes
  0 siblings, 0 replies; 2+ messages in thread
From: Jan Harkes @ 2022-05-20 17:11 UTC (permalink / raw)
  To: Kees Cook; +Cc: linux-kernel, linux-hardening

Looks good to me.

The old code was trying to be too smart for its own good and the type
gets lost by the uc_data = (void *) cast anyway.

Jan


On Fri, May 20, 2022 at 09:59:22AM -0700, Kees Cook wrote:
> GCC 12 does not like seeing a partially allocated structure being used,
> especially when a pointer is being passed out of function scope. Since
> only the struct coda_in_hdr member of union inputArgs is being allocated
> and used, just replace union inputArgs with struct coda_in_hdr.
> 
> ../fs/coda/upcall.c: In function 'coda_upcall':
> ../fs/coda/upcall.c:801:22: warning: array subscript 'union inputArgs[0]' is partly outside array bounds of 'unsigned char[20]' [-Warray-bounds]
>   801 |         sig_inputArgs->ih.opcode = CODA_SIGNAL;
>       |                      ^~
> In file included from ../include/linux/fs.h:45,
>                  from ../include/linux/huge_mm.h:8,
>                  from ../include/linux/mm.h:700,
>                  from ../fs/coda/upcall.c:22:
> In function 'kvmalloc',
>     inlined from 'kvzalloc' at ../include/linux/slab.h:758:9,
>     inlined from 'coda_upcall' at ../fs/coda/upcall.c:794:18:
> ../include/linux/slab.h:750:16: note: object of size 20 allocated by 'kvmalloc_node'
>   750 |         return kvmalloc_node(size, flags, NUMA_NO_NODE);
>       |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../fs/coda/upcall.c: In function 'coda_upcall':
> ../fs/coda/upcall.c:802:22: warning: array subscript 'union inputArgs[0]' is partly outside array bounds of 'unsigned char[20]' [-Warray-bounds]
>   802 |         sig_inputArgs->ih.unique = req->uc_unique;
>       |                      ^~
> In function 'kvmalloc',
>     inlined from 'kvzalloc' at ../include/linux/slab.h:758:9,
>     inlined from 'coda_upcall' at ../fs/coda/upcall.c:794:18:
> ../include/linux/slab.h:750:16: note: object of size 20 allocated by 'kvmalloc_node'
>   750 |         return kvmalloc_node(size, flags, NUMA_NO_NODE);
>       |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Cc: Jan Harkes <jaharkes@cs.cmu.edu>
> Cc: coda@cs.cmu.edu
> Cc: codalist@coda.cs.cmu.edu
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  fs/coda/upcall.c | 20 ++++++++++----------
>  1 file changed, 10 insertions(+), 10 deletions(-)
> 
> diff --git a/fs/coda/upcall.c b/fs/coda/upcall.c
> index 59f6cfd06f96..21e4f5f446b2 100644
> --- a/fs/coda/upcall.c
> +++ b/fs/coda/upcall.c
> @@ -711,7 +711,7 @@ static int coda_upcall(struct venus_comm *vcp,
>  		       union inputArgs *buffer)
>  {
>  	union outputArgs *out;
> -	union inputArgs *sig_inputArgs;
> +	struct coda_in_hdr *ih;
>  	struct upc_req *req = NULL, *sig_req;
>  	int error;
>  
> @@ -791,22 +791,22 @@ static int coda_upcall(struct venus_comm *vcp,
>  	sig_req = kmalloc(sizeof(struct upc_req), GFP_KERNEL);
>  	if (!sig_req) goto exit;
>  
> -	sig_inputArgs = kvzalloc(sizeof(struct coda_in_hdr), GFP_KERNEL);
> -	if (!sig_inputArgs) {
> +	ih = kvzalloc(sizeof(*ih), GFP_KERNEL);
> +	if (!ih) {
>  		kfree(sig_req);
>  		goto exit;
>  	}
>  
>  	error = -EINTR;
> -	sig_inputArgs->ih.opcode = CODA_SIGNAL;
> -	sig_inputArgs->ih.unique = req->uc_unique;
> +	ih->opcode = CODA_SIGNAL;
> +	ih->unique = req->uc_unique;
>  
>  	sig_req->uc_flags = CODA_REQ_ASYNC;
> -	sig_req->uc_opcode = sig_inputArgs->ih.opcode;
> -	sig_req->uc_unique = sig_inputArgs->ih.unique;
> -	sig_req->uc_data = (void *)sig_inputArgs;
> -	sig_req->uc_inSize = sizeof(struct coda_in_hdr);
> -	sig_req->uc_outSize = sizeof(struct coda_in_hdr);
> +	sig_req->uc_opcode = ih->opcode;
> +	sig_req->uc_unique = ih->unique;
> +	sig_req->uc_data = (void *)ih;
> +	sig_req->uc_inSize = sizeof(*ih);
> +	sig_req->uc_outSize = sizeof(*ih);
>  
>  	/* insert at head of queue! */
>  	list_add(&(sig_req->uc_chain), &vcp->vc_pending);
> -- 
> 2.32.0
> 
> 

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

end of thread, other threads:[~2022-05-20 17:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-20 16:59 [PATCH] fs/coda: Do not use partially allocated struct Kees Cook
2022-05-20 17:11 ` Jan Harkes

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.