Commit b4b812e
committed
api.c: fix cgroup_attach_task_pid() behavior
Originally cgroup_attach_task_pid(cgroup, tid), allowed passing of
struct cgroup *cgroup or NULL as a valid argument. In the case of cgroup
being passed, the tid was attached to the cgroup by writing into the
'tasks' file of the cgroup. It gets interesting, when the NULL is
passed, the idea is to migrate the task to the cgroup_root of the
hierarchy of the task is currently running in.
Consider a simple legacy cgroup controller hirerachy:
cpu,cpuacct memory cpuset
/ \ / /
C1 C2 M1 U1
| | / \
t1 t1 UC1 UC2
|
t1
passing NULL:
cpu,cpuacct memory cpuset
/ \ | / | / |
C1 C2 t1 M1 t1 U1 t1
/ \
UC1 UC2
Depending upon the cgroup setup, legacy (only cgroup v1), unified
(cgroup v2) or hybrid (controllers mounted in the cgroup v1 and some
of the functionalities available from cgroup v2). This behavior got
changed in the due course and will cause a segfault, when passed with
NULL as the cgroup.
Reproducer:
int main(int argc, char *argv[])
{
pid_t pid;
int ret;
ret = cgroup_init();
if (ret) {
printf("cgroup_init failed with %s\n",
cgroup_strerror(ret));
exit(1);
}
pid = atoi(argv[1]);
ret = cgroup_attach_task_pid(NULL, pid);
if (ret)
printf("attaching %d to cgroup %s failed\n",
cgroup_strerror(ret));
return ret;
}
Re-introduce the original behavior, that considers all the three
possible cgroup modes and performances of the migration based on the
modes. fscanf() is in-efficient in parsing /proc/<pid>/cgroup fields
with empty values, this is handled by a helper function
proc_pid_cgroup_parser().
It was discovered by the Coverity tool as Dereference after null check:
CID 258278 (#1 of 1): Dereference after null check (FORWARD_NULL).
var_deref_op: Dereferencing null pointer cgroup.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>1 parent 96dce40 commit b4b812e
1 file changed
+154
-26
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2142 | 2142 | | |
2143 | 2143 | | |
2144 | 2144 | | |
2145 | | - | |
| 2145 | + | |
| 2146 | + | |
| 2147 | + | |
| 2148 | + | |
| 2149 | + | |
| 2150 | + | |
| 2151 | + | |
| 2152 | + | |
| 2153 | + | |
| 2154 | + | |
| 2155 | + | |
| 2156 | + | |
| 2157 | + | |
| 2158 | + | |
| 2159 | + | |
| 2160 | + | |
| 2161 | + | |
| 2162 | + | |
| 2163 | + | |
| 2164 | + | |
| 2165 | + | |
| 2166 | + | |
| 2167 | + | |
| 2168 | + | |
| 2169 | + | |
| 2170 | + | |
| 2171 | + | |
| 2172 | + | |
| 2173 | + | |
| 2174 | + | |
| 2175 | + | |
| 2176 | + | |
| 2177 | + | |
| 2178 | + | |
| 2179 | + | |
| 2180 | + | |
| 2181 | + | |
| 2182 | + | |
| 2183 | + | |
| 2184 | + | |
| 2185 | + | |
| 2186 | + | |
| 2187 | + | |
| 2188 | + | |
| 2189 | + | |
| 2190 | + | |
| 2191 | + | |
| 2192 | + | |
| 2193 | + | |
| 2194 | + | |
| 2195 | + | |
| 2196 | + | |
| 2197 | + | |
| 2198 | + | |
| 2199 | + | |
| 2200 | + | |
| 2201 | + | |
| 2202 | + | |
| 2203 | + | |
| 2204 | + | |
| 2205 | + | |
| 2206 | + | |
| 2207 | + | |
| 2208 | + | |
| 2209 | + | |
| 2210 | + | |
| 2211 | + | |
| 2212 | + | |
| 2213 | + | |
| 2214 | + | |
| 2215 | + | |
| 2216 | + | |
| 2217 | + | |
| 2218 | + | |
| 2219 | + | |
| 2220 | + | |
| 2221 | + | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
| 2226 | + | |
| 2227 | + | |
| 2228 | + | |
| 2229 | + | |
| 2230 | + | |
| 2231 | + | |
| 2232 | + | |
2146 | 2233 | | |
| 2234 | + | |
2147 | 2235 | | |
| 2236 | + | |
2148 | 2237 | | |
2149 | 2238 | | |
| 2239 | + | |
2150 | 2240 | | |
2151 | 2241 | | |
2152 | 2242 | | |
| |||
2155 | 2245 | | |
2156 | 2246 | | |
2157 | 2247 | | |
2158 | | - | |
| 2248 | + | |
| 2249 | + | |
| 2250 | + | |
| 2251 | + | |
| 2252 | + | |
| 2253 | + | |
| 2254 | + | |
| 2255 | + | |
2159 | 2256 | | |
2160 | | - | |
2161 | | - | |
2162 | | - | |
| 2257 | + | |
| 2258 | + | |
2163 | 2259 | | |
2164 | | - | |
| 2260 | + | |
2165 | 2261 | | |
2166 | | - | |
2167 | | - | |
| 2262 | + | |
| 2263 | + | |
| 2264 | + | |
| 2265 | + | |
| 2266 | + | |
| 2267 | + | |
| 2268 | + | |
| 2269 | + | |
| 2270 | + | |
| 2271 | + | |
| 2272 | + | |
| 2273 | + | |
| 2274 | + | |
| 2275 | + | |
| 2276 | + | |
| 2277 | + | |
| 2278 | + | |
| 2279 | + | |
| 2280 | + | |
| 2281 | + | |
| 2282 | + | |
| 2283 | + | |
| 2284 | + | |
| 2285 | + | |
| 2286 | + | |
| 2287 | + | |
| 2288 | + | |
| 2289 | + | |
| 2290 | + | |
| 2291 | + | |
| 2292 | + | |
| 2293 | + | |
2168 | 2294 | | |
2169 | | - | |
| 2295 | + | |
2170 | 2296 | | |
| 2297 | + | |
| 2298 | + | |
| 2299 | + | |
| 2300 | + | |
| 2301 | + | |
| 2302 | + | |
| 2303 | + | |
| 2304 | + | |
2171 | 2305 | | |
2172 | | - | |
2173 | | - | |
2174 | | - | |
2175 | | - | |
2176 | | - | |
2177 | | - | |
2178 | 2306 | | |
2179 | 2307 | | |
2180 | | - | |
2181 | | - | |
| 2308 | + | |
| 2309 | + | |
2182 | 2310 | | |
2183 | | - | |
| 2311 | + | |
2184 | 2312 | | |
2185 | 2313 | | |
2186 | 2314 | | |
2187 | 2315 | | |
2188 | | - | |
| 2316 | + | |
2189 | 2317 | | |
2190 | 2318 | | |
2191 | 2319 | | |
2192 | 2320 | | |
2193 | | - | |
2194 | | - | |
| 2321 | + | |
| 2322 | + | |
2195 | 2323 | | |
2196 | | - | |
2197 | | - | |
| 2324 | + | |
| 2325 | + | |
2198 | 2326 | | |
2199 | | - | |
| 2327 | + | |
2200 | 2328 | | |
2201 | 2329 | | |
2202 | 2330 | | |
2203 | | - | |
2204 | | - | |
| 2331 | + | |
| 2332 | + | |
2205 | 2333 | | |
2206 | 2334 | | |
2207 | 2335 | | |
| |||
0 commit comments