ipc/sem.c: replace kvmalloc/memset with kvzalloc and use struct_size
authorGustavo A. R. Silva <gustavo@embeddedor.com>
Fri, 8 Mar 2019 00:30:26 +0000 (16:30 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 8 Mar 2019 02:32:02 +0000 (18:32 -0800)
Use kvzalloc() instead of kvmalloc() and memset().

Also, make use of the struct_size() helper instead of the open-coded
version in order to avoid any potential type mistakes.

This code was detected with the help of Coccinelle.

Link: http://lkml.kernel.org/r/20190131214221.GA28930@embeddedor
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
ipc/sem.c

index a188d1b064ea02cf2d7ebcbdae3304d935c0782c..7da4504bcc7cfd8c57b897e66608fa3890427fc3 100644 (file)
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -488,18 +488,14 @@ static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
 static struct sem_array *sem_alloc(size_t nsems)
 {
        struct sem_array *sma;
-       size_t size;
 
        if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
                return NULL;
 
-       size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
-       sma = kvmalloc(size, GFP_KERNEL);
+       sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL);
        if (unlikely(!sma))
                return NULL;
 
-       memset(sma, 0, size);
-
        return sma;
 }