Skip to content

Commit 5d8ca75

Browse files
committed
Lift cpuset Capsicum checks into a subroutine.
Otherwise the same checks are duplicated across four different system call implementations, cpuset_(get|set)(affinity|domain)(). No functional change intended. MFC after: 1 week Sponsored by: The FreeBSD Foundation
1 parent 6cee159 commit 5d8ca75

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

sys/kern/kern_cpuset.c

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,25 @@ cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
15821582
return (0);
15831583
}
15841584

1585+
/*
1586+
* In Capability mode, the only accesses that are permitted are to the current
1587+
* thread and process' CPU and domain sets.
1588+
*/
1589+
static int
1590+
cpuset_check_capabilities(struct thread *td, cpulevel_t level, cpuwhich_t which,
1591+
id_t id)
1592+
{
1593+
if (IN_CAPABILITY_MODE(td)) {
1594+
if (level != CPU_LEVEL_WHICH)
1595+
return (ECAPMODE);
1596+
if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1597+
return (ECAPMODE);
1598+
if (id != -1)
1599+
return (ECAPMODE);
1600+
}
1601+
return (0);
1602+
}
1603+
15851604
#ifndef _SYS_SYSPROTO_H_
15861605
struct cpuset_args {
15871606
cpusetid_t *setid;
@@ -1739,15 +1758,9 @@ kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
17391758

17401759
if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
17411760
return (ERANGE);
1742-
/* In Capability mode, you can only get your own CPU set. */
1743-
if (IN_CAPABILITY_MODE(td)) {
1744-
if (level != CPU_LEVEL_WHICH)
1745-
return (ECAPMODE);
1746-
if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1747-
return (ECAPMODE);
1748-
if (id != -1)
1749-
return (ECAPMODE);
1750-
}
1761+
error = cpuset_check_capabilities(td, level, which, id);
1762+
if (error != 0)
1763+
return (error);
17511764
size = cpusetsize;
17521765
mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
17531766
error = cpuset_which(which, id, &p, &ttd, &set);
@@ -1856,15 +1869,9 @@ kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
18561869

18571870
if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
18581871
return (ERANGE);
1859-
/* In Capability mode, you can only set your own CPU set. */
1860-
if (IN_CAPABILITY_MODE(td)) {
1861-
if (level != CPU_LEVEL_WHICH)
1862-
return (ECAPMODE);
1863-
if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1864-
return (ECAPMODE);
1865-
if (id != -1)
1866-
return (ECAPMODE);
1867-
}
1872+
error = cpuset_check_capabilities(td, level, which, id);
1873+
if (error != 0)
1874+
return (error);
18681875
mask = malloc(cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
18691876
error = copyin(maskp, mask, cpusetsize);
18701877
if (error)
@@ -1987,15 +1994,9 @@ kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
19871994
if (domainsetsize < sizeof(domainset_t) ||
19881995
domainsetsize > DOMAINSET_MAXSIZE / NBBY)
19891996
return (ERANGE);
1990-
/* In Capability mode, you can only get your own domain set. */
1991-
if (IN_CAPABILITY_MODE(td)) {
1992-
if (level != CPU_LEVEL_WHICH)
1993-
return (ECAPMODE);
1994-
if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1995-
return (ECAPMODE);
1996-
if (id != -1)
1997-
return (ECAPMODE);
1998-
}
1997+
error = cpuset_check_capabilities(td, level, which, id);
1998+
if (error != 0)
1999+
return (error);
19992000
mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
20002001
bzero(&outset, sizeof(outset));
20012002
error = cpuset_which(which, id, &p, &ttd, &set);
@@ -2122,15 +2123,9 @@ kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
21222123
if (policy <= DOMAINSET_POLICY_INVALID ||
21232124
policy > DOMAINSET_POLICY_MAX)
21242125
return (EINVAL);
2125-
/* In Capability mode, you can only set your own CPU set. */
2126-
if (IN_CAPABILITY_MODE(td)) {
2127-
if (level != CPU_LEVEL_WHICH)
2128-
return (ECAPMODE);
2129-
if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
2130-
return (ECAPMODE);
2131-
if (id != -1)
2132-
return (ECAPMODE);
2133-
}
2126+
error = cpuset_check_capabilities(td, level, which, id);
2127+
if (error != 0)
2128+
return (error);
21342129
memset(&domain, 0, sizeof(domain));
21352130
mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
21362131
error = copyin(maskp, mask, domainsetsize);

0 commit comments

Comments
 (0)