Rework cpu affinity parsing
The CPU_SET_S api is pretty bad. In particular, it has a parameter for the size
of the array, but operations which take two (CPU_EQUAL_S) or even three arrays
(CPU_{AND,OR,XOR}_S) still take just one size. This means that all arrays must
be of the same size, or buffer overruns will occur. This is exactly what our
code would do, if it received an array of unexpected size over the network.
("Unexpected" here means anything different from what cpu_set_malloc() detects
as the "right" size.)
Let's rework this, and store the size in bytes of the allocated storage area.
The code will now parse any number up to 8191, independently of what the current
kernel supports. This matches the kernel maximum setting for any architecture,
to make things more portable.
Fixes #12605.
This commit is contained in:
+10
-25
@@ -219,7 +219,7 @@ static int property_get_cpu_affinity(
|
||||
assert(reply);
|
||||
assert(c);
|
||||
|
||||
return sd_bus_message_append_array(reply, 'y', c->cpuset, CPU_ALLOC_SIZE(c->cpuset_ncpus));
|
||||
return sd_bus_message_append_array(reply, 'y', c->cpu_set.set, c->cpu_set.allocated);
|
||||
}
|
||||
|
||||
static int property_get_timer_slack_nsec(
|
||||
@@ -1566,37 +1566,22 @@ int bus_exec_context_set_transient_property(
|
||||
|
||||
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
|
||||
if (n == 0) {
|
||||
c->cpuset = cpu_set_mfree(c->cpuset);
|
||||
c->cpuset_ncpus = 0;
|
||||
cpu_set_reset(&c->cpu_set);
|
||||
unit_write_settingf(u, flags, name, "%s=", name);
|
||||
} else {
|
||||
_cleanup_free_ char *str = NULL;
|
||||
size_t ncpus;
|
||||
const CPUSet set = {(cpu_set_t*) a, n};
|
||||
|
||||
str = cpu_set_to_string(a, n);
|
||||
str = cpu_set_to_string(&set);
|
||||
if (!str)
|
||||
return -ENOMEM;
|
||||
|
||||
ncpus = CPU_SIZE_TO_NUM(n);
|
||||
|
||||
if (!c->cpuset || c->cpuset_ncpus < ncpus) {
|
||||
cpu_set_t *cpuset;
|
||||
|
||||
cpuset = CPU_ALLOC(ncpus);
|
||||
if (!cpuset)
|
||||
return -ENOMEM;
|
||||
|
||||
CPU_ZERO_S(n, cpuset);
|
||||
if (c->cpuset) {
|
||||
CPU_OR_S(CPU_ALLOC_SIZE(c->cpuset_ncpus), cpuset, c->cpuset, (cpu_set_t*) a);
|
||||
CPU_FREE(c->cpuset);
|
||||
} else
|
||||
CPU_OR_S(n, cpuset, cpuset, (cpu_set_t*) a);
|
||||
|
||||
c->cpuset = cpuset;
|
||||
c->cpuset_ncpus = ncpus;
|
||||
} else
|
||||
CPU_OR_S(n, c->cpuset, c->cpuset, (cpu_set_t*) a);
|
||||
/* We forego any optimizations here, and always create the structure using
|
||||
* cpu_set_add_all(), because we don't want to care if the existing size we
|
||||
* got over dbus is appropriate. */
|
||||
r = cpu_set_add_all(&c->cpu_set, &set);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
unit_write_settingf(u, flags, name, "%s=%s", name, str);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user