Tom Hromatka [Fri, 16 Sep 2022 17:18:27 +0000 (11:18 -0600)]
tests: Merge libcgroup-tests repo back into libcgroup repo
Merge the libcgroup-tests repo back into the main libcgroup repo.
The submodules logic has been deleted and the tests are now directly
hosted within the libcgroup repo.
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Sam James [Wed, 14 Sep 2022 14:15:43 +0000 (08:15 -0600)]
configure.ac: fix bashism
configure scripts need to be runnable with a POSIX-compliant /bin/sh.
On many (but not all!) systems, /bin/sh is provided by Bash, so errors
like this aren't spotted. Notably Debian defaults to /bin/sh provided
by dash which doesn't tolerate such bashisms as '=='.
This retains compatibility with bash.
Fixes configure warnings/errors like:
```
checking whether to build static libraries... no
./configure: 14089: test: xno: unexpected operator
checking for x86_64-pc-linux-gnu-g++... x86_64-pc-linux-gnu-g++
```
Signed-off-by: Sam James <sam@gentoo.org> Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 4fc7dc1036676901d555fe296cca661a783e405b)
tools/cgsnapshot: fix wrong array size in is_ctrl_on_list()
GCC throws -Wstringop-overflow warning:
CC cgsnapshot-cgsnapshot.o
cgsnapshot.c: In function 'parse_controllers':
cgsnapshot.c:540:53: warning: 'is_ctlr_on_list' accessing 16777216 bytes in a region of size 409600 [-Wstringop-overflow=]
540 | if ((!(flags & FL_LIST) || (is_ctlr_on_list(controllers, cont_names))) &&
| ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cgsnapshot.c:540:53: note: referencing argument 2 of type 'char(*)[4096]'
cgsnapshot.c:495:12: note: in a call to function 'is_ctlr_on_list'
495 | static int is_ctlr_on_list(char controllers[CG_CONTROLLER_MAX][FILENAME_MAX],
| ^~~~~~~~~~~~~~~
cgsnapshot.c:560:37: warning: 'is_ctlr_on_list' accessing 16777216 bytes in a region of size 409600 [-Wstringop-overflow=]
560 | if ((!(flags & FL_LIST) || (is_ctlr_on_list(controllers, cont_names))) &&
| ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cgsnapshot.c:560:37: note: referencing argument 2 of type 'char(*)[4096]'
cgsnapshot.c:495:12: note: in a call to function 'is_ctlr_on_list'
495 | static int is_ctlr_on_list(char controllers[CG_CONTROLLER_MAX][FILENAME_MAX],
| ^~~~~~~~~~~~~~~
the warning is seen due to the mismatch in the array size of the second
argument passed to is_ctlr_on_list() from parse_controllers(). Fix,
this long standing warning by chaging the size of the second function
argument in is_ctrl_on_list().
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 4362c8d32d5d8813a9a40bebe744aeaec5fbf953)
Kamalesh Babulal [Wed, 31 Aug 2022 21:08:04 +0000 (15:08 -0600)]
src/api.c: support /proc mounted with subset=pid
/proc filesystem can be mounted with subset=pid as one of its mount
options. This option hides all the top-level files and directories,
those are not related to processes. The cgroup v1 filesystem depends
on the /proc/cgroups to populate the cgroups controllers and will fail
during the cgroup_init() phase, when not available, whereas cgroup v2
considers this as a deprecated file and recommends reading the list of
controller from <unified mount point>/cgroup.controllers[1].
Support this valid /proc mount point only when the system is booted with
the unified mode and will fail to initialize in the case we find the
cgroup v1 mounted, i.e, the system booted with legacy or hybrid mode.
Kamalesh Babulal [Wed, 31 Aug 2022 21:07:15 +0000 (15:07 -0600)]
api.c: use /proc/self/mounts
Use /proc/self/mount instead of /proc/mounts, which is a symbolic link
to the former. The symbolic link creation can be traced back to Linux
Kernel commit 59c7572e82d6 ("proc: remove fs/proc/proc_misc.c"). Also,
the /proc/mounts are not available in the cases where the /proc is
mounted with subset=pid option.
As per kernel docs filesystems/procs.rst:
"subset=pid hides all top level files and directories in the procfs that
are not related to tasks."
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 16be308b037bbe11a54b692c227e160f1132eabe)
Kamalesh Babulal [Mon, 29 Aug 2022 21:03:56 +0000 (15:03 -0600)]
api.c: fix file open in cg_chmod_path()
In cg_chmod_path(), the commit 96db65fbb529 ("api.c: fix TOCTOU in
cg_chmod_path()), converted the file operations from stat -> fstat and
chmod -> fchmod to fix a Coverity warning. The newly replaced file
operations operate on file descriptors and hence introduced a side
effect of opening the file at the wrong code block, that would only work
as expected when the caller calls cg_chmod_path() with owner_is_umask
set.
Fix it by moving the file operation out of the conditional block, so it
works in both of the cases of owner_is_umask being set or unset.
Fixes: 96db65fbb529 ("api.c: fix TOCTOU in cg_chmod_path()) Suggested-by: Tom Hromatka <tom.hromatka@oracle.com> Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 91cf2e4b7ceb19c02d66af717c18d7bc64fa5df9)
Kamalesh Babulal [Wed, 24 Aug 2022 18:49:03 +0000 (12:49 -0600)]
api.c: fix TOCTOU in cg_chmod_path()
Fix TOCTOU warning, reported by Coverity Tool:
CID 258267 (#1 of 1): Time of check time of use (TOCTOU).
fs_check_call: Calling function stat to perform check on path.
in cg_chmod_path(), the file name is stat() and not immediately followed
by the file operation. One way to fix it, open the file and use the file
descriptor to manipulate the file.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 8b9665c29cb812c255687d6e5cb5262a6fe30a40)
Kamalesh Babulal [Wed, 10 Aug 2022 17:10:54 +0000 (11:10 -0600)]
daemon/cgrulesengd: check the bytes read in cgre_receive_unix_domain_msg()
Fix ignoring the number of bytes read, warning reported by Coverity
tool:
CID 258286 (#1 of 1): Ignoring number of bytes read (CHECKED_RETURN).
check_return: read(int, void *, size_t) returns the number of bytes
read, but it is ignored.
In cgre_receive_unix_domain_msg(), the number of bytes read() is
ignored, while reading from the flag value of the pid. Coverity warns on
not checking the number of bytes read, fix it.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit ed283eab173addd3b5c2dd666de8f3086125e106)
Kamalesh Babulal [Wed, 10 Aug 2022 16:59:19 +0000 (10:59 -0600)]
api.c: add precision to fscanf(), in cgroup_get_current_controller_path()
Fix calling risky function warning, reported by Coverity tool:
CID 258301 (#1 of 1): Calling risky function
(DC.STREAM_BUFFER)dont_call: fscanf assumes an arbitrarily long string,
so callers must use correct precision specifiers or never use fscanf.
As per secure coding standard, using '%s' in the fscanf() is not
recommend, hence fix it by using the precision of macro
FILENAME_MAX borrowed from Linux Kernel for the maximum
allowed controller/subsys_name length.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit ac05eb49c498aa5047070a4620456fa7a10e1910)
TJH: Small fix in the commit comment.
api.c: add precision to fscanf(), in cgroup_populate_controllers()
Fix calling risky function warning, reported by Coverity tool:
CID 258305 (#1 of 1): Calling risky function
(DC.STREAM_BUFFER)dont_call: fscanf assumes an arbitrarily long string,
so callers must use correct precision specifiers or never use fscanf.
As per secure coding standard, using '%s' in the fscanf() is not
recommend, hence fix it by using the precision of macro
MAX_CGROUP_TYPE_NAMELEN borrowed from Linux Kernel for the maximum
allowed controller/subsys_name length.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 04169a9bef0ee77c18cd5a2e2d9f6e49f6057e32)
api.c: add precision to fscanf(), in cgroup_get_all_controller_next()
Fix calling risky function warning, reported by Coverity tool:
CID 258300 (#1 of 1): Calling risky function
(DC.STREAM_BUFFER)dont_call: fscanf assumes an arbitrarily long string,
so callers must use correct precision specifiers or never use fscanf.
As per secure coding standard, using '%s' in the fscanf() is not
recommend, hence fix it by using the precision of macro
MAX_CGROUP_TYPE_NAMELEN borrowed from Linux Kernel for the maximum
allowed controller/subsys_name length.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 4036f4fe4334572a4669dfece01ff24dbdd56477)
daemon/cgrulesengd: check the bytes read in cgre_receive_unix_domain_msg()
Fix ignoring the number of bytes read, warning reported by Coverity
tool:
CID 258286 (#2 of 2): Ignoring number of bytes read (CHECKED_RETURN).
check_return: read(int, void *, size_t) returns the number of bytes
read, but it is ignored.
In cgre_receive_unix_domain_msg(), the number of bytes read() is
ignored, while reading from the flag value from the socket. Coverity
warns on not checking the number of bytes read, fix it.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 23cee33b4d2c856c1c442f7dfed1237a5e52e2a3)
api.c: check the bytes read in cgroup_register_unchanged_process()
Fix ignoring the number of bytes read, warning reported by Coverity
tool:
CID 258288 (#1 of 1): Ignoring number of bytes read (CHECKED_RETURN).
check_return: read(int, void *, size_t) returns the number of bytes
read, but it is ignored.
In cgroup_register_unchanged_process(), the number of byte read/written
using read()/write() are ignored but coverity it warns about the read()
only, let's fix it.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 9d266da61515bb47b889a93229302e545da207fe)
tools/cgsnapshot: fix out-of-bounds write in parse_controllers()
Fix Out-of-bounds write warning, reported by Coverity tool:
CID 258289 (#2 of 2): Out-of-bounds write (OVERRUN)16. overrun-local:
Overrunning array controllers of 100 4096-byte elements at element index
100 (byte offset 413695) using index max (which evaluates to 100).
there are chances, that the index variable max dereferences controller
array might be over the array size of 100. Add upper bound checks
to index variable max, so that it doesn't overrun the controller array.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit de951069e925a697a047c6327a8578ef6b5d2353)
api.c: check for invalid error code in cgroup_strerror()
Fix array overflow warning, reported by the Coverity tool:
CID 258309 (#1 of 1): Out-of-bounds read (OVERRUN). overrun-local:
Overrunning array cgroup_strerror_codes of 32 8-byte elements at element
index 49999 (byte offset 399999) using index code % ECGROUPNOTCOMPILED
(which evaluates to 49999).
there are chances of users passing error codes, resulting in crossing
the upper bound of the cgroup_strerror_codes[], fix it by introducing
bound checks.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 37fab4e36b3785698fcc8af14e624cf1e182c183)
Fix a resource leak, reported by the Coverity tool:
CID 258291 (#1 of 1): Resource leak (RESOURCE_LEAK)8. leaked_storage:
Variable handle going out of scope leaks the storage it points to.
In get_cv_value(), currently, we goto end label, on the failure of
strdup() before closing the handle, leaking the resource. Fix it by
removing the goto, that allows the code flow to close the handle and
execute the code under the end label.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit d8bf0b38a75ed06520d05d130c0c92234df8665f)
api: null terminate readlink buffer in cgroup_get_procname_from_procfs()
Fix readlink buffer null termination warning, reported by Coverity tool:
CID 258273 (#2 of 2): String not null terminated (STRING_NULL).
string_null: Passing unterminated string buf to strdup, which expects a
null-terminated string.
As per the man pages (man 2 readlink):
"readlink() does not append a null byte to buf. It will (silently)
truncate the contents (to a length of bufsiz characters), in case the
buffer is too small to hold all of the contents."
Explicitly null terminate the buffer passed to readlink() in
cgroup_get_procname_from_procfs()
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 397554c9df86ec7d06e017b877573859c60c258f)
api: null terminate readlink buffer in cg_get_procname_from_proc_cmdline()
Fix readlink buffer null termination warning, reported by Coverity tool:
CID 258276 (#1 of 1): String not null terminated (STRING_NULL).
string_null: Passing unterminated string buf_cwd to strcat, which
expects a null-terminated string.
As per the man pages (man 2 readlink):
"readlink() does not append a null byte to buf. It will (silently)
truncate the contents (to a length of bufsiz characters), in case the
buffer is too small to hold all of the contents."
Explicitly null terminate the buffer passed to readlink() in
cg_get_procname_from_proc_cmdline().
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 23bdfd2326a01675a0eac9ab7b601b4a505d219c)
api.c: fix string termination in cgroup_get_procname_from_procfs()
Fix non-terminated string warning, reported by Coverity tool:
CID 258273 (#2 of 2): String not null terminated (STRING_NULL)6.
string_null: Passing unterminated string buf to strdup, which expects a
null-terminated string.
use snprintf() instead of sprintf(), to string terminate
cgroup_get_procname_from_procfs().
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 422c0300583a7b450838e4936ba0d51584354831)
wrapper.c: null terminate string in cgroup_new_cgroup()
Fix non-terminated string warning, reported by Coverity tool:
CID 258290 (#1 of 1): String not null terminated (STRING_NULL)46.
string_null: Passing unterminated string aux_cgroup->name to
cgroup_create_cgroup, which expects a null-terminated string.
The call patch leading to this warning:
config.c::cgroup_config_create_template_group()
- wrapper.c::cgroup_new_cgroup()
fix it by null terminating the string.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 239b3c4154e8129b62db0f1743f3e947e6932871)
tools/cgsnapshot: add ret value fix in parse_controllers()
Fix the unused ret value warning, reported by Coverity:
CID 258275 (#1 of 1): Unused value (UNUSED_VALUE)returned_value:
Assigning value from display_controller_data(controllers, program_name)
to ret here, but that stored value is overwritten before it can be used.
The parse_controllers(), doesn't check for errors in the value returned
by display_controller_data(). The return value might very well contain
an error, that might go unnoticed. Fix it by adding a check for the
return value.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit f2f107c8b3ef0f699c0a3f77aeebbcbeb4f1b8b1)
With Ubuntu 5.15.0-1014 kernel, there are a few changes to the output
of cgget -g cpu:<cgroup> provides for both cgroup V1 and V2:
cgroup V1:
----------
- new stat files cpu.cfs_burst_us. cpu.idle
cgroup V2:
----------
- new stat files cpu.idle, cpu.max.burst
adopt these changes while looking for expected out based on the cgroup
version. Also, convert the expected out templates into per cgroup
version list[], making it easier to match using a for loop, instead of
nested if else. Using a list also makes it easier to append any new
changes to the output template.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 7c998caddcd8236fe4191841e361b401697fb777)
With Ubuntu 5.15.0-1014 kernel, there are a few changes to the output
of cgget -g cpu:<cgroup> provides for both cgroup V1 and V2:
cgroup V1:
----------
- new stat files cpu.cfs_burst_us. cpu.idle
cgroup V2:
----------
- new stat files cpu.idle, cpu.max.burst
adopt these changes while looking for expected out based on the cgroup
version. Also, convert the expected out templates into per cgroup
version list[], making it easier to match using a for loop, instead of
nested if else. Using a list also makes it easier to append any new
changes to the output template.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit cf0bd90e3d2f0cd30fd13602f814cbf05fa0d557)
With Ubuntu 5.15.0-1014 kernel, there are a few changes to the output
of cgget -g cpu:<cgroup> provides for both cgroup V1 and V2:
cgroup V1:
----------
- new stat files cpu.cfs_burst_us, cpu.idle
cgroup V2:
----------
- new stat files cpu.idle, cpu.max.burst
adopt these changes while looking for expected out based on the cgroup
version. Also, convert the expected out templates into per cgroup
version list[], making it easier to match using a for loop, instead of
nested if else. Using a list also makes it easier to append any new
changes to the output template.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 96b533988a10ba877673adebe70fc5808844e862)
test-020: fix issues on unsuccessful no-container runs
Running the test with --no-container, might cause an issue with useradd
or groupadd command if the home directory of the test user exists/test
user group exists.
The home directory of the user doesn't get removed by default on all
distros, so explicitly pass the '-r' flag to userdel to remove the
directory. Whereas user group might exist due to unsuccessful test
runs, address it by passing '-f' flags to groupadd command, that
returns success even if the group exists, that will be removed
by the groupdel command at the exit.
This is not an issue with containers test cases, because they are
created and destroyed for every test case execution.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit e650f509840c4134a706e6600e3b909be07b249b)
test-019: fix issues on unsuccessful no-container runs
Running the test with --no-container, might cause an issue with useradd
or groupadd command if the home directory of the test user exists/test
user group exists.
The home directory of the user doesn't get removed by default on all
distros, so explicitly pass the '-r' flag to userdel to remove the
directory. Whereas user group might exist due to unsuccessful test
runs, address it by passing '-f' flags to groupadd command, that
returns success even if the group exists, that will be removed
by the groupdel command at the exit.
This is not an issue with containers test cases, because they are
created and destroyed for every test case execution.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 59260dfeec1753540fd9ddccc0515aa0ddf2d6ed)
api.c: add ret value fix in cgroup_change_cgroup_flags()
Fix the unused ret value warning, reported by Coverity:
CID 258304 (#1 of 1): Unused value (UNUSED_VALUE)returned_value:
Assigning value from cgroup_create_template_group(newdest, tmp, flags)
to ret here, but that stored value is overwritten before it can be used.
It turns out that cgroup_change_cgroup_flags(), calls the function
cgroup_create_template_group() to a cgroup based on a template, which
may fail and we don't check the return value for the error. This fix
adds the check and bails out on error.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit c46591c5cf0d85b551668d98b33c86a68853b3d1)
Fix copy into fixed size buffer warning, reported by Coverity tool:
CID 258284 (#4 of 4): Copy into fixed size buffer (STRING_OVERFLOW)1.
fixed_size_dest: You might overrun the 4096-character fixed-size string
tmp_val by copying tok without checking the length.
In indent_multiline_value(), warned about the usage of strcat(), that
might overwrite the string. Fix it by replacing strcat() -> strncat()
in the function.
Signed-off-by: Kamalesh Babulal kamalesh.babulal@oracle.com Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 9c8724c1e543026453365aaeca7ece93dc12dac7)
wrapper.c: return on failure in cgroup_add_all_controllers()
Add missing goto statement, to return on failure, reported by Coverity
tool:
CID 258281 (#1 of 1): Unused value (UNUSED_VALUE)assigned_value:
Assigning value ECGINVAL to ret here, but that stored value is
overwritten before it can be used.
the tool reported about unused ret value, but it turned out that the
NULL ret value is for failed attempt to add a controller to the desired
cgroup and needs bailing out after losing the handle. Fix it by
introducing the goto statement in cgroup_add_all_controllers()
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
TJH: Also fix unrelated grammar error near this change.
(cherry picked from commit 83d64a4474366938e1459403eb0c98c3d3b7ee00)
Fix non-terminated string warnings, reported by the Coverity tool:
CID 258293 (#2 of 2): Copy into fixed size buffer (STRING_OVERFLOW).
fixed_size_dest: You might overrun the 32-character fixed-size string
config_namespace_table[namespace_table_index].name by copying name
without checking the length.
fix one another similar string
config_namespace_table[namespace_table_index].mount.path in the same
function cgroup_config_insert_into_namespace_table() by explicitly
terminating by appending '\0';
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 8eee916573cd9a67713dd645d644d22759f39a69)
Fix non-terminated string warning, reported by Coverity tool:
CID 258266 (#1 of 1): String not null-terminated (STRING_NULL).
string_null: Passing unterminated string con to strtok, which expects a
null-terminated string.
Fix the warning in create_cgroup_from_name_value_pairs(), by adding
'\0'.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 0fd476b6a6f309ccd3b0c473c844a27e7620cddd)
config: cgroup_config_insert_into_mount_table() use strncpy()
Fix copy into fixed size buffer warning, reported by Coverity tool:
CID 258282 (#2 of 2): Copy into fixed size buffer (STRING_OVERFLOW)9.
fixed_size_dest: You might overrun the 4096-character fixed-size string
config_mount_table[config_table_index].mount.path by copying mount_point
without checking the length.
parameter_as_source: Note: This defect has an elevated risk because the
source argument is a parameter of the current function
Also, convert config_mount_table[config_table_index].name strcpy to
strncpy.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 4c8e4fd284b4677e5e9bab4f9e13b0866020b499)
calling "hasmntopt" to determine if the controller name exists in
"mntopt", may cause errors because of "hasmntopt" only match substring.
cpu controller may incorrectly match to cpuset when cpuset mount info
appeared before cpu,cpuacct in "/proc/mounts", so we need to validate
the last character to make sure the controller name matches exactly.
Signed-off-by: liupan <490021209@qq.com> Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit a18ae861e77f106517df203e30a810ba59b082d9)
Tom Hromatka [Thu, 9 Jun 2022 19:05:23 +0000 (13:05 -0600)]
api.c: Fix handling of full cg_mount_table[]
Commit 9ce90c7edd28 ("api.c: fix segfault in cgroup_populate_mount_points()")
added logic to handle the case when there are 100+ cgroup mounts and
not overflow the cg_mount_table[]. But elsewhere in the libcgroup
code, it's expected that the last entry in the cg_mount_table[] has
a null name entry.
When the cg_mount_table[] is full, make the name of the last entry
null so that loops know to exit. Also, add a couple bail out points
in cgroup_populate_mount_points() to ensure that we don't write
beyond the end of the table.
Depending upon the order in which the tests are run, this failure can
manifest itself as follows:
$ cat tests/ftests/ftests-nocontainer.sh.log
free(): invalid pointer
./ftests-nocontainer.sh: line 18: 199390 Aborted
(core dumped) ./ftests.py -l 10 -L "$START_DIR/ftests-nocontainer.py.log" --no-container -n Libcg"$RANDOM"
FAIL ftests-nocontainer.sh (exit status: 134)
Fixes: 9ce90c7edd28 ("api.c: fix segfault in cgroup_populate_mount_points()") Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com> Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
(cherry picked from commit 50de38f821f5ea367f9a92a802a45659dc45614d)
wrapper: null terminated string in cgroup_add_controller()
Fix non-terminated string warning, reported by Coverity tool:
CID 258271 (#1 of 1): String not null terminated (STRING_NULL).
string_null: Passing unterminated string cgc->name to strcmp, which
expects a null-terminated string.
This issue was reported following the path src/abstraction-common.c:
- cgroup_convert_cgroup()
- cgroup_add_controller()
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Tue, 31 May 2022 22:55:47 +0000 (16:55 -0600)]
api: null terminated string in cgroup_get_controller_next()
Fix non-terminated string warnings, reported by Coverity tool:
CID 258299 (#1-2 of 2): String not null terminated (STRING_NULL).
string_null: Passing unterminated string controller.path to strcmp,
which expects a null-terminated string
This issue was reported following the path src/tools/cgsnapshot.c:
- parse_controllers()
- cgroup_get_controller_begin()
- cgroup_get_controller_next()
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Tue, 31 May 2022 22:52:53 +0000 (16:52 -0600)]
config: add missing ret value check from getgrnam_r()
Fix Unchecked return values from library, reported by Coverity tool:
CID 258287 (#1 of 1): Unchecked return value from library
(CHECKED_RETURN).
check_return: Calling getgrnam_r(value, group, buffer, 20480UL,
&group_buffer) without checking return value. This library function may
fail and return an error code.
CID 258303 (#1 of 1): Unchecked return value from library
(CHECKED_RETURN).
check_return: Calling getgrnam_r(value, group, buffer, 20480UL,
&group_buffer) without checking return value. This library function may
fail and return an error code.
Coverity expects us to check for return value from getgrnam_r(), instead
of the current for group_buffer != NULL. Which is right, let's make
Coverity happy by moving the check to return value.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Fri, 27 May 2022 20:58:13 +0000 (14:58 -0600)]
api: fix coverity warning about uninitialized variable
CID 258269 (#1 of 1): Uninitialized scalar variable (UNINIT).
uninit_use: Using uninitialized value version.
In _cgroup_create_cgroup(), the (cg_version_t)version is uninitialized
and might be read the version in false path. It worked until now
because the version is assigned in the true path and given that its enum
the checks are not bounded by range, but rather specific.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Wed, 25 May 2022 13:50:07 +0000 (07:50 -0600)]
tools/cgget: fix coverity warning about double free
Fix double free warning, reported by Coverity tool:
CID 258297 (#1 of 1): Double free (USE_AFTER_FREE). double_free:
Calling closedir frees pointer dir which has already been freed.
As per the man page, the closedir(), closes the directory stream
associated with the dirp, but is ambiguous about if dirp is set to NULL
or not. Coverity answers that with the above report, that dirp is not
NULL and hence the double free.
Fixes: fea1ab8b45d7 ("tools/cgget: fix coverity warning about resource leak") Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 703bbb8554ce882eb461e16c389d20a356f095de)
Kamalesh Babulal [Wed, 25 May 2022 13:42:08 +0000 (07:42 -0600)]
api.c: fix coverity out of bounds read warning
Fix out of bounds read, reported by Coverity tool:
CID 1412156 (#1 of 1): Out-of-bounds read (OVERRUN).
overrun-local: Overrunning array cgroup_strerror_codes of 30 8-byte
elements at element index 49999 (byte offset 399999) using index
code % ECGROUPNOTCOMPILED (which evaluates to 49999).
Reproducer:
-----------
$ cat cgrp-strerr.c
int main(void)
{
int err = ECGNONEMPTY;
fprintf(stderr, "%s\n", cgroup_strerror(err));
return 0;
}
without the patch:
-----------------
$ ./cgrp-stderr
(null)
with the patch:
---------------
$ ./cgrp-stderr
Failed to remove a non-empty group
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
There are scenarios where running tests as part of the make check is
not desirable, like rpmbuild. The build systems might not have the
environment to run the tests and might fail to build the packages.
This patch introduces, --enable-tests as a configure option, which
is set by default and can be disabled using the --disable-tests flag.
When disabled, the configure will skip creating the Makefiles for the
tests/* directories and in the top-level Makefile the SUBDIRS omits
tests. For example, consider the rpmbuild case to build rpm with
--disable-tests passed:
1. ./bootstrap.sh
2. ./configure ./configure --enable-opaque-hierarchy="name=systemd"
--disable-tests
3. make check
4. make dist
5. cp libcgroup-2.0.2.tar.gz ~/rpmbuild/SOURCES
6. append --disable-tests to configure in libcgroup.spec
7. rpmbuild -ba libcgroup.spec
this will skip running the test/*, during the build, avoiding unexpected
failures
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Fix uninitialized scalar variable, reported by Coverity tool:
CID 1412125 (#1 of 1): Uninitialized scalar variable (UNINIT).
uninit_use_in_call: Using uninitialized element of array *controllers
when calling is_ctlr_on_list.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 4a92b3ca0fcca18f69ee75d4627db7171d22d217)
Fix uninitialized scalar variable, reported by Coverity tool:
CID 1488715 (#1 of 1): Uninitialized scalar variable (UNINIT).
uninit_use_in_call: Using uninitialized value *src_cg_path as argument
to %s when calling fprintf.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit b652d0e431835c5566be4cdf27172445bfe9d015)
Fix uninitialized scalar variable, reported by Coverity tool:
CID 1488707 (#1 of 1): Uninitialized scalar variable (UNINIT).
uninit_use_in_call: Using uninitialized value *cont_names when calling
print_controller_mount.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 059095386e150710beb93c71977031c1245c176e)
Kamalesh Babulal [Wed, 18 May 2022 17:19:52 +0000 (11:19 -0600)]
tests/016: fix the cause in prereqs
Match the cause message in the prereqs(), for containers check with
other test cases. This helps the automated scripts to check/match
for the right errors.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit bb8ab50f2ab16d2808c3ba0bf6f20303f382b930)
Fix a wrong argument size, reported by Coverity tool:
CID 1412122 (#1 of 1): Wrong sizeof argument (SIZEOF_MISMATCH)
suspicious_sizeof: Passing argument 8UL
/* sizeof (struct ext_cgroup_record *) */ to function calloc and then
casting the return value to struct ext_cgroup_record * is suspicious.
Also, fix the typo in the variable name, that's being allocated memory.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 2045fcd6b3428f1b1ca226974259e00399049472)
Kamalesh Babulal [Mon, 16 May 2022 22:06:44 +0000 (16:06 -0600)]
workflows/CI: upgrade CodeQL actions used to v2
CodeQL warns about deprecating v1 by Dec 2022. Upgrade the CodeQL
actions, as per the recommendation made in the upgrade guide at:
https://github.blog/changelog/2022-04-27-code-scanning-deprecation-of-codeql-action-v1/
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 887bae0df3c68c73375b822931acc7aff5add87e)
Kamalesh Babulal [Wed, 11 May 2022 16:21:46 +0000 (10:21 -0600)]
configure: fix the extra spaces in --enable-bindings
Extra spaces between the default action assignment for the feature
--enable-bindings feature, fails with:
./configure: line 12510: with_bindings: command not found
fix the issue by removing the extra spaces in the assignment.
api.c: fix segfault while processing cgroup mount points
The cgroup v1 and cgroup v2 mount points get read from /proc/mounts in
cgroup_process_v1_mnt()/cgroup_process_v2_mnt() respectively and gets
populated into cg_mount_table[]. The size of cg_mount_table[] is set
to CG_CONTROLLER_MAX, that's defined as 100 and if the system has more
than CG_CONTROLLER_MAX, unique mount points, it will segfault while
processing them. Fix this by checking, the mount point count after
processing every mount entry and bailout in case it reaches
CG_CONTROLLER_MAX.
The issue can be reproduced using, following simple bash commands on
cgroup v1:
1. sudo for i in $(seq 0 100); do sudo mkdir /name$i; done
2. sudo for i in $(seq 0 100); do sudo mount -t cgroup -o
none,name=named$i none /name$i; done
3. sudo cgget -g <controller>:<existing cgroup name>
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Thu, 27 Jan 2022 19:47:55 +0000 (12:47 -0700)]
api.c: add support for cgroup.threads
cgroup_build_tasks_procs_path(), that builds the proc path for moving
the tid, doesn't check for the cgroup type and appends cgroup.procs for
both cgroup v1/v2. With cgroup v2, this results in building a wrong
proc path if the cgroup type is threaded. This patch adds support to
check for the cgroup v2 type and build the proc path according to the
below table:
cgroup.type | proc file | Description |
----------------+----------------+------------------------------------+
domain | cgroup.procs | regular cgroup, all threads gets |
| | moved to the cgroup. |
domain threaded | cgroup.procs | root of threaded subtree, all |
| | threads gets moved to the cgroup. |
threaded | cgroup.threads | cgroup under threaded subtree, |
| | single threads with matching tid, |
| | gets moved to the cgroup. |
domain invalid | N/A | invalid state of a cgroup under the|
| | threaded subtree. |
Closes: https://github.com/libcgroup/libcgroup/issues/64 Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
cherry picked from commit 4c577f91050b1ad00e5ed2b2abb40fc4978db996)
Kamalesh Babulal [Tue, 18 Jan 2022 15:35:04 +0000 (08:35 -0700)]
api.c: cache cgrules with CGFLAG_USECACHE flag
pam_cgroup.so plugin uses /etc/cgrules.conf to assign processes
to the requested cgroup. This works well with active
cgrulesengd but in the instances where cgrulesengd is disabled,
the rules are not read and cached by default. Fix this is by
reloading and caching the rules when called with CGFLAG_USECACHE
flag.
Fixes: https://github.com/libcgroup/libcgroup/issues/84 Reported-by: Kent Overstreet <kent.overstreet@gmail.com> Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
TJH: Minor commit comment changes
(cherry picked from commit 80a0bd4b8873df38060309e775d609dcc219531f)
Ingo van Lil [Thu, 16 Dec 2021 20:34:00 +0000 (13:34 -0700)]
Initialize cgroup version for named V1 hierarchy
Commit 3aac9889 added the 'version' field in cg_mount_table_s and
initializes it in cgroup_process_v1_mnt(). The same initialization is
required in a second place dealing with named hierarchies.
Signed-off-by: Ingo van Lil <inguin@gmx.de> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit d0a1979e8078e7669daf1713bb97ef408a2befbc)
Tom Hromatka [Tue, 14 Dec 2021 21:43:30 +0000 (14:43 -0700)]
ftests: Add a test to get/set cgroup.type
Add a functional test that can get/set cgroup.type on a
cgroup v2 filesystem.
-----------------------------------------------------------------
Test Results:
Run Date: Dec 06 14:40:46
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
-------------------------------------------
setup 0.00
035-cgset-set_cgroup_type.py 2.30
teardown 0.00
-------------------------------------------
Total Run Time 2.30
Ross Burton [Fri, 3 Dec 2021 15:38:27 +0000 (08:38 -0700)]
automake: idiomatically mark test libraries as convenience libraries
Instead of installing then deleting, mark the libraries which are only
used at build time and should not be installed as noinst, so they don't
get installed at all.
Signed-off-by: Ross Burton <ross.burton@arm.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 1932bd3c46fc5f3532d6583ccc2ccd4c8d8530a2)
Tom Hromatka [Fri, 12 Nov 2021 18:32:03 +0000 (18:32 +0000)]
github: Run test 028 last
A merge conflict moved the running of test 028 before
running `make check`. Run test 028-cgclear-basic_cgclear.py
last because it is destructive to the cgroup hierarchy.
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Tom Hromatka [Fri, 12 Nov 2021 13:56:37 +0000 (06:56 -0700)]
automake: Don't install testing libraries
libcgset and libcgfortesting are libraries that are used by the
unit test framework and are not intended to be installed when
`make install` is invoked. Delete them from the install path.
Closes: https://github.com/libcgroup/libcgroup/issues/62 Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 6c941e8b26fbd6613c96ed65e08290aa21ff33de)
Tom Hromatka [Fri, 12 Nov 2021 13:52:51 +0000 (06:52 -0700)]
automake: Link libcgset with libcgroup
When linking with the -Wl,--no-undefined flag enabled, libcgset
fails due to undefined symbols. Fix this by enabling this flag
and linking with libcgroup.la
Closes: https://github.com/libcgroup/libcgroup/issues/66 Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit b4015e3f96fc430029a37e94edc151e16727e8f1)
Kamalesh Babulal [Thu, 28 Oct 2021 20:26:54 +0000 (14:26 -0600)]
ftests.py: fix test results timing alignment
The current width length of 35 characters, used as alignment for the
first column in the timing results of ftests.sh.log is not enough and
results in misaligned output. Fix it by assigning the columns format
strings width to the longest filename, calculated just before executing
every testcase. This avoids adjusting the width every time, with newer
testcases. Also, fix the banner separator lines to match the width of
the filename column + 15 (time), dynamically.
Kamalesh Babulal [Thu, 21 Oct 2021 21:25:55 +0000 (15:25 -0600)]
github: re-arrange workflow run commands
The current workflow executes the functional tests from the make check
called at the top-level source directory. Though this runs the tests
case, it also has an unintended side effect of re-running gunit tests
for every functional test combination in the workflow. Fix it, by
re-arranging the function test run commands, to change into
tests/ftests directory before executing make check.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 348f50bed7bfab1a63a649b4a65120fb03a7d3c2)
Ross Burton [Fri, 10 Sep 2021 17:42:20 +0000 (11:42 -0600)]
configure.ac: check for the library containing fts_open
The musl C library doesn't provide fts.h[1], so libcgroup doesn't
compile with musl. However, there is a standalone implementation of fts
for musl users[2] which can be used.
Use AC_SEARCH_LIBS to search for fts_open, which will check if it is part
of libc, and if not look in libfts, then set LIBS if needed.
Signed-off-by: Ross Burton <ross.burton@arm.com>
[TJH: minor formatting changes to match existing configure.ac] Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 27ba904355575153308bdc0fa48344ef1be55ca1)
Replace __BEGIN_DECLS and __END_DECLS with extern "C".
The macros __BEGIN_DECLS and __END_DECLS are a GNU-ism found in
glibc and uClibc, but not musl. We replace them by the more general
extern "C" { ... } block exposed only if we have __cplusplus.
Forward ported from a patch by Anthony G. Basile <blueness@gentoo.org>.
Signed-off-by: Alex Xu (Hello71) <alex_y_xu@yahoo.ca> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 215974e67a24edf9097507e8707190b6a42c7207)
Tom Hromatka [Mon, 17 May 2021 14:22:48 +0000 (14:22 +0000)]
ftests: Add a cgexec test
Add a basic cgexec test
-----------------------------------------------------------------
Test Results:
Run Date: May 03 19:56:02
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
---------------------------------------------------------
setup 11.87
034-cgexec-basic_cgexec.py 2.55
teardown 0.00
---------------------------------------------------------
Total Run Time 14.42
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Tomasz Kłoczko [Wed, 12 May 2021 20:35:40 +0000 (20:35 +0000)]
automake: replace INCLUDES by CPPFLAGS
automake shows warnings like below:
src/Makefile.am:16: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
src/Makefile.am: installing 'build-aux/depcomp'
src/bindings/Makefile.am:2: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
src/daemon/Makefile.am:3: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
src/pam/Makefile.am:1: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
src/tools/Makefile.am:3: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
Signed-off-by: Tomasz Kłoczko <kloczek@github.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>