Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965956AbcKKJJn (ORCPT ); Fri, 11 Nov 2016 04:09:43 -0500 Received: from mailout3.samsung.com ([203.254.224.33]:49613 "EHLO mailout3.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S936249AbcKKJJk (ORCPT ); Fri, 11 Nov 2016 04:09:40 -0500 X-AuditID: cbfee61b-f796f6d000004092-a8-58258ad295da From: Sachin Shukla To: "Eric W. Biederman" , Kees Cook , Serge Hallyn , Andrey Vagin , linux-kernel@vger.kernel.org, sachiniiitm@gmail.com Cc: ravikant.s2@samsung.com, p.shailesh@samsung.com, ashish.kalra@samsung.com, vidushi.koul@samsung.com Subject: [PATCH] Kernel: Improvement in code readability when memdup_user_nul() fails. Date: Fri, 11 Nov 2016 14:37:15 +0530 Message-id: <1478855235-26233-1-git-send-email-sachin.s5@samsung.com> X-Mailer: git-send-email 1.7.9.5 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrFLMWRmVeSWpSXmKPExsVy+t9jAd1LXaoRBj9Xi1jcn/aN3WLF0mss Fv+3tbBbnOnOtbi8aw6bxY4prSwWTQ2z2S0eLJrLbHH+wjl2i78XL7E5cHnMbrjI4rFz1l12 j2u7Iz0mHf7M5NG3ZRWjx+dNch5TDrWzBLBHudlkpCampBYppOYl56dk5qXbKoWGuOlaKCnk Jeam2ipF6PqGBCkplCXmlAJ5RgZowME5wD1YSd8uwS3j+Z0ZzAU7eCtm3LzM1MB4h6uLkZND QsBE4ue+44wQtpjEhXvr2boYuTiEBJYySnz9t5EVwvnJKPGmdQlYFZuAtsSH/vksIAkRgQuM ElualzB3MXJwMAtkSrQ0VICYwgIREiuXK4CUswioSlxZ9o0JxOYVcJXYMHUSI0iJhICCxJxJ NhMYuRcwMqxilEgtSC4oTkrPNcpLLdcrTswtLs1L10vOz93ECA7nZ9I7GA/vcj/EKMDBqMTD K5GlGiHEmlhWXJl7iFGCg1lJhLelFSjEm5JYWZValB9fVJqTWnyI0RRo/0RmKdHkfGCs5ZXE G5qYm5gbG1iYW1qaGCmJ8zbOfhYuJJCeWJKanZpakFoE08fEwSnVwOiTfkfU4MXM93UhNlKf AsTE4o3+fTm35OCjiGNSen4sKzffF/385OSz51d2r0pwOXty1bVK52PhqanK6fMc/nwOPeDD omK+6cX0POuV+3+vVuL/ferJ0uniq3o4loU5e2uvsWu/s0rFQO3ctMnR9/z3rwzomrfvUuUU 8cvNt7w0uFp3vfqf1LtDiaU4I9FQi7moOBEAMky1Vn0CAAA= X-MTR: 20000000000000000@CPGS Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1659 Lines: 59 From: "Sachin Shukla" There is no need to call kfree() if memdup_user_nul() fails, as no memory was allocated and the error in the error-valued pointer should be returned. Signed-off-by: Sachin Shukla --- kernel/user_namespace.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 86b7854..a0ffbf0 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -672,28 +672,31 @@ static ssize_t map_write(struct file *file, const char __user *buf, */ mutex_lock(&userns_state_mutex); - ret = -EPERM; /* Only allow one successful write to the map */ - if (map->nr_extents != 0) - goto out; + if (map->nr_extents != 0) { + mutex_unlock(&userns_state_mutex); + return -EPERM; + } /* * Adjusting namespace settings requires capabilities on the target. */ - if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN)) - goto out; + if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN)) { + mutex_unlock(&userns_state_mutex); + return -EPERM; + } /* Only allow < page size writes at the beginning of the file */ - ret = -EINVAL; - if ((*ppos != 0) || (count >= PAGE_SIZE)) - goto out; + if ((*ppos != 0) || (count >= PAGE_SIZE)) { + mutex_unlock(&userns_state_mutex); + return -EINVAL; + } /* Slurp in the user data */ kbuf = memdup_user_nul(buf, count); if (IS_ERR(kbuf)) { - ret = PTR_ERR(kbuf); - kbuf = NULL; - goto out; + mutex_unlock(&userns_state_mutex); + return PTR_ERR(kbuf); } /* Parse the user data */ -- 1.7.9.5