Skip to content

Commit 825332e

Browse files
Arjan van de Venxjamesmorris
authored andcommitted
capabilities: simplify bound checks for copy_from_user()
The capabilities syscall has a copy_from_user() call where gcc currently cannot prove to itself that the copy is always within bounds. This patch adds a very explicity bound check to prove to gcc that this copy_from_user cannot overflow its destination buffer. Signed-off-by: Arjan van de Ven <arjan@linux.intel.com> Acked-by: James Morris <jmorris@namei.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: James Morris <jmorris@namei.org>
1 parent a27ab9f commit 825332e

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

‎kernel/capability.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
238238
SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
239239
{
240240
struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
241-
unsigned i, tocopy;
241+
unsigned i, tocopy, copybytes;
242242
kernel_cap_t inheritable, permitted, effective;
243243
struct cred *new;
244244
int ret;
@@ -255,8 +255,11 @@ SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
255255
if (pid != 0 && pid != task_pid_vnr(current))
256256
return -EPERM;
257257

258-
if (copy_from_user(&kdata, data,
259-
tocopy * sizeof(struct __user_cap_data_struct)))
258+
copybytes = tocopy * sizeof(struct __user_cap_data_struct);
259+
if (copybytes > sizeof(kdata))
260+
return -EFAULT;
261+
262+
if (copy_from_user(&kdata, data, copybytes))
260263
return -EFAULT;
261264

262265
for (i = 0; i < tocopy; i++) {

0 commit comments

Comments
 (0)