Skip to content

Commit 112cca0

Browse files
cypharbrauner
authored andcommitted
sched_getattr: port to copy_struct_to_user
sched_getattr(2) doesn't care about trailing non-zero bytes in the (ksize > usize) case, so just use copy_struct_to_user() without checking ignored_trailing. Signed-off-by: Aleksa Sarai <[email protected]> Link: https://lore.kernel.org/r/20241010-extensible-structs-check_fields-v3-2-d2833dfe6edd@cyphar.com Signed-off-by: Christian Brauner <[email protected]>
1 parent 424a55a commit 112cca0

File tree

1 file changed

+2
-40
lines changed

1 file changed

+2
-40
lines changed

kernel/sched/syscalls.c

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,45 +1076,6 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
10761076
return copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
10771077
}
10781078

1079-
/*
1080-
* Copy the kernel size attribute structure (which might be larger
1081-
* than what user-space knows about) to user-space.
1082-
*
1083-
* Note that all cases are valid: user-space buffer can be larger or
1084-
* smaller than the kernel-space buffer. The usual case is that both
1085-
* have the same size.
1086-
*/
1087-
static int
1088-
sched_attr_copy_to_user(struct sched_attr __user *uattr,
1089-
struct sched_attr *kattr,
1090-
unsigned int usize)
1091-
{
1092-
unsigned int ksize = sizeof(*kattr);
1093-
1094-
if (!access_ok(uattr, usize))
1095-
return -EFAULT;
1096-
1097-
/*
1098-
* sched_getattr() ABI forwards and backwards compatibility:
1099-
*
1100-
* If usize == ksize then we just copy everything to user-space and all is good.
1101-
*
1102-
* If usize < ksize then we only copy as much as user-space has space for,
1103-
* this keeps ABI compatibility as well. We skip the rest.
1104-
*
1105-
* If usize > ksize then user-space is using a newer version of the ABI,
1106-
* which part the kernel doesn't know about. Just ignore it - tooling can
1107-
* detect the kernel's knowledge of attributes from the attr->size value
1108-
* which is set to ksize in this case.
1109-
*/
1110-
kattr->size = min(usize, ksize);
1111-
1112-
if (copy_to_user(uattr, kattr, kattr->size))
1113-
return -EFAULT;
1114-
1115-
return 0;
1116-
}
1117-
11181079
/**
11191080
* sys_sched_getattr - similar to sched_getparam, but with sched_attr
11201081
* @pid: the pid in question.
@@ -1159,7 +1120,8 @@ SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
11591120
#endif
11601121
}
11611122

1162-
return sched_attr_copy_to_user(uattr, &kattr, usize);
1123+
kattr.size = min(usize, sizeof(kattr));
1124+
return copy_struct_to_user(uattr, usize, &kattr, sizeof(kattr), NULL);
11631125
}
11641126

11651127
#ifdef CONFIG_SMP

0 commit comments

Comments
 (0)