Commit Graph

296 Commits

Author SHA1 Message Date
Sam James
5af3944440 librc: missing includes from IWYU 2023-01-29 11:32:22 -06:00
Sam James
d6a5264a9d librc: fix 'init' memory leak
From scan-build w/ clang-16.0.0_pre20230107:
```
../src/librc/librc.c:759:14: warning: Potential leak of memory pointed to by 'init' [unix.Malloc]
                                                return false;
                                                       ^~~~~
```
2023-01-25 00:48:26 -05:00
Sam James
0525de4f18 librc-depend: fix -Wmismatched-dealloc
Despite this being a 'deptree', it's actually
xmalloc'd in the same function (rc_deptree_update),
and so should be free'd, not rc_deptree_free'd,
as rc_deptree_load* wasn't used to allocate it.

```
[71/213] Compiling C object src/librc/librc.so.1.p/librc-depend.c.o
../src/librc/librc-depend.c: In function ‘rc_deptree_update’:
../src/librc/librc-depend.c:1077:9: warning: ‘rc_deptree_free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
 1077 |         rc_deptree_free(deptree);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from ../src/shared/misc.h:29,
                 from ../src/librc/librc.h:57,
                 from ../src/librc/librc-depend.c:21:
In function ‘xmalloc’,
    inlined from ‘rc_deptree_update’ at ../src/librc/librc-depend.c:775:12:
../src/shared/helpers.h:64:23: note: returned from ‘malloc’
   64 |         void *value = malloc(size);
      |                       ^~~~~~~~~~~~
```

This fixes #563.
2022-12-07 11:35:16 -06:00
William Hubbs
5ffa1c7fd7 librc: fix resource leaks 2022-05-08 17:10:44 -05:00
Sam James
7b2e8f9efd librc: mark rc_deptree_* functions as mallocs
(This is analogous to the rc_stringlist change.)

This gives a hint to the compiler that allocations (return values)
from this function should be paired with a corresponding dealloc/free
function.

Signed-off-by: Sam James <sam@gentoo.org>
2022-05-08 14:56:26 -05:00
Sam James
000a2c19b4 librc: mark stringlist functions as warn_unused_result
Signed-off-by: Sam James <sam@gentoo.org>
2022-05-08 14:56:26 -05:00
Sam James
17de4e5dfd librc: mark stringlist functions as mallocs
This gives a hint to the compiler that allocations (return values)
from this function should be paired with a corresponding dealloc/free
function

In this case, it means that every rc_stringlist that rc_stringlist_new()
returns should eventually be freed by calling rc_stringlist_free(ptr)
where ptr is the relevant rc_stringlist.

We have to add a test for this into the build system
because only GCC supports this for now. In future, we might
be able to use meson's has_function_attribute (it does support
'malloc', just not AFAICT 'malloc with arguments').

Signed-off-by: Sam James <sam@gentoo.org>
2022-05-08 14:56:26 -05:00
Sam James
1afcc37803 librc: fix potential use-after-free
Both 'pkg' and 'local' are referenced within list,
so we can't free them yet.

Signed-off-by: Sam James <sam@gentoo.org>
2022-05-08 14:56:26 -05:00
Sam James
c4f8b1559c librc: fix double-free
'services' is still referenced by the list
which gets returned. We can't free it.

Thanks to GCC 11's -fanalyzer.

Signed-off-by: Sam James <sam@gentoo.org>
2022-05-08 14:56:26 -05:00
Sam James
fbefd68b2d librc: fix mismatched allocs
Signed-off-by: Sam James <sam@gentoo.org>
2022-05-08 14:56:26 -05:00
Sam James
14835a2262 librc: fix memory leak in rc_services_in_runlevel_stacked
Seen on running rc-status.

```
=================================================================
==14636==ERROR: LeakSanitizer: detected memory leaks

Indirect leak of 72 byte(s) in 3 object(s) allocated from:
    #0 0x7f443412dcb7 in __interceptor_malloc /usr/src/debug/sys-devel/gcc-11.2.1_p20220312/gcc-11-20220312/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x7f443400c727 in xmalloc ../src/includes/helpers.h:64
    #2 0x7f443400d1f4 in rc_stringlist_add ../src/librc/librc-stringlist.c:32
    #3 0x7f4433fecc34 in get_runlevel_chain ../src/librc/librc.c:390
    #4 0x7f4433fedc00 in rc_runlevel_stacks ../src/librc/librc.c:519
    #5 0x7f4433ff1d8e in rc_services_in_runlevel_stacked ../src/librc/librc.c:976
    #6 0x55be0e8f9517 in main ../src/rc/rc-status.c:407
    #7 0x7f44334736cf in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
```

Signed-off-by: Sam James <sam@gentoo.org>
2022-05-08 14:56:26 -05:00
Josh Soref
3054046e18 multiple spelling and typo fixes
This fixes #516.
2022-04-30 21:32:32 -05:00
William Hubbs
391d12db48 migrate fully to meson build system
- drop old build system
- move shared include and source files to common directory
- drop "rc-" prefix from shared include and source files
- move executable-specific code to individual directories under src
- adjust top-level .gitignore file for new build system

This closes #489.
2022-04-06 10:51:55 -05:00
William Hubbs
5e9797035b fix libkvm dependency for *bsd 2021-12-30 18:45:39 -06:00
Matt Whitlock
8ffc4162e2 code style: remove space after unary "not" operator
There are no semantic changes in this commit.

Suggested-by: Mike Frysinger <vapier@gentoo.org>
See: https://github.com/OpenRC/openrc/pull/435#pullrequestreview-727035394
2021-12-21 01:48:04 -05:00
Mike Frysinger
301161a7a4 use HEAD in git URIs to point to the default branch
This makes the URIs shorter and dynamic: whatever the default branch
the repo uses will be used.
2021-12-20 20:07:00 -05:00
Mike Frysinger
04d8234844 drop unused copyright variables
The existing copyright notices in the file headers cover these vars.
2021-12-20 20:04:59 -05:00
Sam James
015c83761d src/librc/librc-daemon.c: add missing include for kill (fix uclibc)
Fixes: #471
Fixes: #473
2021-11-12 22:59:03 -06:00
Sam James
0091a628c5 src/librc/librc-misc.c: add missing include for fileno (fix uclibc)
Fixes: #471
Fixes: #473
2021-11-12 22:58:22 -06:00
William Hubbs
444e44eb9d build: change root_prefix to rootprefix 2021-09-06 14:30:30 -05:00
William Hubbs
d6622a1156 add meson build files
Closes #116.
Closes #171.
Closes #172.
Closes #175.
2021-09-04 16:01:29 -05:00
William Hubbs
0a97bad6c9 librc: fix rc_runlevel_exists return for empty string
This function should return false if the runlevel is an empty string.

    X-Gentoo-Bug: 803536
    X-Gentoo-Bug-URL: https://bugs.gentoo.org/803536
    Closes: https://github.com/OpenRC/openrc/pull/431
2021-08-13 10:28:44 -05:00
William Hubbs
ef76a663bc stop namespaced services with specified pids
The previous fix excludes PIDs of processes running in a different namespace
regardless of whether the PID has been explicitly stored in a PID file mentioned
in the --pidfile parameter.  The correct behavior is to only exclude the pid if
it is not stored in a pidfile.

X-Gentoo-Bug: 776010
X-Gentoo-Bug-URL: https://bugs.gentoo.org/776010
2021-03-22 11:34:41 -05:00
Martin Wilke
7ddc281ab6 Fix build with Clang
This fixes #313.
2019-07-24 12:32:23 -05:00
William Hubbs
44f5a72d1a remove hidden-visibility.h
I am removing this on the advice of a member of the Gentoo toolchain
team. It was explained to me that this doesn't offer any significant
benefits to OpenRC.

If anyone ffeels differently, please open a pull request reverting
this and adding an explanation of what it does and how to know which
functions to mark hidden in the future.

This fixes #301.
2019-02-26 17:47:55 -06:00
William Hubbs
028da5c2e3 librc: fix potential buffer overflow in pid_is_argv
This fixes #299.
2019-02-23 17:44:07 -06:00
William Hubbs
d8dbb890aa Revert "src/librc/librc-daemon.c: fix buffer overrun in pid_is_argv"
This reverts commit 084877eb52.
The mentioned commit caused some systems to have some services reported
as crashed.

This fixes #297.
This fixes #298.
2019-02-23 16:24:55 -06:00
Georgy Yakovlev
7478c104fc librc/librc-depend.c: fix NULL pointer dereference
In some cases deptree or depinfo can be NULL, check
before dereferencing.

Fixes https://github.com/OpenRC/openrc/issues/293
Fixes https://github.com/OpenRC/openrc/pulls/294
X-Gentoo-Bug: 659906
X-Gentoo-Bug-URL: https://bugs.gentoo.org/659906
2019-02-21 17:49:54 -06:00
Edan Bedrik
b2b2c57a38 librc: fix realpath() return value check
This fixes #226.
2019-02-12 17:56:17 -06:00
philhofer
3e00fbc9b0 fix leading whitespace
Clean up code indented with mixed tabs and spaces.
No actual code changes.

This fixes #280.
2018-12-27 13:13:09 -06:00
philhofer
846e460075 fix potential out-of-bounds reads
readlink(3) does not nul-terminate the result it sticks
into the supplied buffer. Consequently, the code

  rc = readlink(path, buf, sizeof(buf));

does not necessarily produce a C string.

The code in rc_find_pid() produces some C strings this way
and passes them to strlen() and strcmp(), which can lead
to an out-of-bounds read.

In this case, since the code already takes care to
zero-initialize the buffers before passing them
to readlink(3), only allow sizeof(buf)-1 bytes to
be returned.

(While fixing this issue, I fixed two other locations that
used the same problematic pattern.)

This fixes #270.
2018-12-27 11:28:27 -06:00
philhofer
084877eb52 src/librc/librc-daemon.c: fix buffer overrun in pid_is_argv
The contents of /proc/<pid>/cmdline are read into
a stack buffer using

  bytes = read(fd, buffer, sizeof(buffer));

followed by appending a null terminator to the buffer with

  buffer[bytes] = '\0';

If bytes == sizeof(buffer), then this write is out-of-bounds.

Refactor the code to use rc_getfile instead, since PATH_MAX
is not the maximum size of /proc/<pid>/cmdline. (I hit this
issue in practice while compiling Linux; it tripped the
stack-smashing protector.)

This is roughly the same buffer overflow condition
that was fixed by commit 0ddee9b7d2
This fixes #269.
2018-12-24 11:55:48 -06:00
William Hubbs
ed8b768c4a fix compiler warnings 2018-12-21 12:06:15 -06:00
Zac Medico
84ed570eae librc: fix EACCES errno false-positive crash
Use errno != EACCES to fix false-positive for non-root users
with grsecurity kernels.

Fixes: 37e2944272 ("librc: Add check for crashed state")
This fixes #237
2018-08-06 17:39:52 -05:00
William Hubbs
3a803b3135 librc-daemon.c: fix memory leaks 2018-06-27 12:06:19 -05:00
William Hubbs
72df51e17b librc-daemon: convert most snprintf calls to xasprintf 2018-06-22 15:41:25 -05:00
William Hubbs
b2f5531194 librc-misc: convert snprintf calls to xasprintf 2018-06-20 17:45:01 -05:00
William Hubbs
37e2944272 librc: Add check for crashed state
In rc_service_state,, call rc_service_daemons_crashed to check for
a crashed daemon if the service is started.
2018-05-22 12:19:41 -05:00
William Hubbs
4e0eace837 librc: Add crashed state 2018-05-22 12:19:22 -05:00
Will Miles
a7c99506d9 Fix repeated dependency cache rebuild if clock skewed
rc_deptree_update_needed would return early as soon as it found
any file newer than the existing dependency cache.  Unfortunately,
the first file found may not be the newest one there; so the
clock skew workaround in rc-misc:_rc_deptree_load would be given
a timestamp that was still too old.

This fix forces a full scan of all relevant files, so as to
ensure that we return a timestamp that will allow the clock skew
fix to operate.   The runtime cost is no worse than the case where
the cache is up to date (ie. we must check every possible file).

This fixes #161.
2017-11-30 13:56:54 -06:00
William Hubbs
971e82784c rc_find_pids: namespace fix
Ignore namespaces if there are errors reading either the pid namespace
for the current process or the process we aare testing.

This fixes https://github.com/openrc/openrc/issues/180.
2017-11-13 16:54:57 -06:00
William Hubbs
cf429ee359 rc_service_value_set: remove the option if NULL is the value
This allows the equivalent of "unsetting" a value for a service.
2017-10-26 12:54:37 -05:00
William Hubbs
f5acc66db7 rc_find_pids: ignore pids that are not in our pid namespace
X-Gentoo-Bug: 634634
X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=634634
2017-10-24 10:37:37 -05:00
William Hubbs
13ca79856e add init process
openrc-init.c and openrc-shutdown.c are based on code which was written by
James Hammons <jlhamm@acm.org>, so I would like to publically
thank him for his work.
2017-04-06 17:13:59 -05:00
Benda Xu
c304522131 Clean up warnings that can use the _unused macro
X-Gentoo-Bug: 604666
X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=604666
2017-01-04 17:18:35 -06:00
Doug Freed
45aa36cc62 librc: detect loops in stacked runlevels and abort
This fixes #109.
X-Gentoo-Bug: 558700
X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=558700
2016-12-19 18:24:31 -06:00
William Hubbs
ca8c29ee60 librc: fix Docker auto detection
The original auto detection of Docker containers assumed the presence of
a container environment variable. However, Docker-1.12 does not
implement this, and I'm not sure which versions of docker implemented
it.

The new test is for the presence of a file named .dockerenv in the
root directory.
2016-07-31 12:24:56 -05:00
William Hubbs
3a1262703f Remove the DEBUG_MEMORY macro
This fixes #43.
2016-07-14 16:52:57 -05:00
William Hubbs
69f052b611 librc: Complain when a real and virtual service have the same name 2016-01-21 15:58:03 -06:00
Doug Freed
4c814a0a28 librc: handle rc_sys="prefix" even if we weren't built with a prefix
This probably isn't needed, but it mimics old behavior.
2016-01-19 00:09:20 -06:00