Commit Graph

1254 Commits

Author SHA1 Message Date
Jan Pokorný
41ae3e1267 Memleak fixes (#194)
* memleak: ipc_socket: properly dispose local-scoped strndup values

Leaking memory was only possible when using filesystem sockets (see
use_filesystem_sockets function) and either:
- client is deliberately disconnecting from a server (continued run
  imposes a risk of exhausting memory)
- server is deliberately disconnecting from its client (ditto, but
  more substantial risk due to the common shared-resource nature
  of the server)

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>

* memleak: ipc_socket: properly dispose inter-function strdup values

Leaking memory was only possible when the server accepted the client,
but didn't get (or was too shy) to talk to it prior to proceeding with
a disconnect.

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>

* ipc_socket: care to explain what's going on with file name inference

Related to the code parts at hand, there was an investigation/fix in
the past, initiated by "make check" failure on FreeBSD 9 [rhbz#1256701].
Unfortunately, not only the magic constant being modified was not
explained in 1908e6c, but (one can derive because of a lack of solid
background of what's going on here, which might have caused that),
it was modified incorrectly at one instance (see also [PR165 comment]),
which was then reinstated in 7ebcb3d.

So, finally de-mystify those magic constants.  Also break the symmetry
between the client/server further with depending on the canonical
"request socket" alias at the server side (the former worked equally but
it was unnecessarily confusing and there's a risk this artificial alias
will get removed in the future).

[rhbz#1256701] https://bugzilla.redhat.com/1256701
[PR165 comment] https://github.com/ClusterLabs/libqb/issues/165#issuecomment-142949541

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
2017-05-18 11:29:15 +01:00
Christine Caulfield
536b4c25ed doc: Clarify IPC caution text
It was not clear to non-native English speakers.

Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
Reviewed by: Jan Pokorný <jpokorny@redhat.com>
2017-05-02 10:29:44 +01:00
Chrissie Caulfield
41a24a3df7 Allow Linux to use filesystem sockets (#248)
* IPC: Allow filesystem sockets to be chosen at run-time on Linux

Most of this patch came from Andrew Beekhof.
Keep a global variable that decides whether or not to use filesystem sockets
or abstract sockets for IPC connections. This variable is set by the presence of a file (default /etc/libqb/force-filesystem-sockets).

* tests: Fix test_ipcc_truncate_when_unlink_fails_shm test using FS sockets

When using filesystem sockets, the
test_ipcc_truncate_when_unlink_fails_shm test always fails, this was
because the unlink() call is wrapped to fail and so it never cleans up
the old version of the socket.
The fix is to preemptively remove the file before unlink gets wrapped.

* doc: Explain the force-filesystem-sockets option

Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
2017-04-28 16:13:02 +01:00
Chrissie Caulfield
63d79c93cc Merge pull request #250 from jnpkrn/test-ipc-random-name
test: Fix random number generation in IPC tests
2017-04-28 14:47:19 +01:00
Jan Pokorný
d69cc7b3f6
test: Fix random number generation in IPC tests
The sockets are named using a random() suffix in at attempt to isolate
concurrent test.  However random() always returns the same random number
by design ... unless pre-seeded with some value being unique enough for
the particular execution.

Borrowing the most of the above message from original "srandom" fix by
Chrissie who also discovered this issue (nice!), I thought it would be
more viable if we encoded such "unique enough" variables directly to
IPC name being generated, not relying on pseudorandom generators in any
way.  Hence this other fix.

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
2017-04-28 14:55:06 +02:00
Christine Caulfield
f38a877889 doc: Remove unclear part of the clarification on IPC threading.
Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
2017-04-05 09:49:45 +01:00
Christine Caulfield
60122a4a4e doc: clarify thread-safety (or not) in IPC doc
Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
2017-03-24 13:44:17 +00:00
Christine Caulfield
59eacf07dd loop: Fix splint error
Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
2017-03-06 14:31:57 +00:00
Christine Caulfield
e336b716cc loop: Also set signals changed in qb_loop_signal_mod() back to SIG_DFL
Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
2017-02-24 16:36:35 +00:00
Christine Caulfield
c751993c90 loop: don't override external signal handlers
qb_loop_signal_add() used to set any signals it wasn't managing
back to SIG_DFL. This is unfriendly behaviour in a library.

Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
Reviewed-by: Jan Pokorný <jpokorny@redhat.com>
2017-02-24 16:23:43 +00:00
Christine Caulfield
afdff97f1a [tests] Fix qb_rb_chunk_peek test so it's consistent with qb_rb_read
Now that the library code is too.

Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
2017-01-31 10:41:29 +00:00
Christine Caulfield
a7faca1682 [ringbuffer] Return error from peek if RB is corrupted.
This should prevent libqb from looping in the server if the
ringbuffer gets corrupted. Instead the client will be disconnected.

Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
2017-01-31 09:47:27 +00:00
Chrissie Caulfield
75345a0a23 Merge pull request #242 from jnpkrn/travis-issue-234
CI: travis: fix du -> df and capture it also directly from test
2016-12-16 15:16:29 +00:00
Jan Pokorný
991ef77d11
build: tito: unify custom.py with pacemaker's version 2016-12-16 15:48:25 +01:00
Jan Pokorný
524042d797
CI: travis: fix du -> df and capture it also directly from test 2016-12-16 15:48:10 +01:00
Chrissie Caulfield
5a54a51883 Merge pull request #241 from jnpkrn/PR240-addendum
PR #240 + addendum (drop syslog-tests opt-in switch)
2016-12-16 14:21:52 +00:00
Jan Pokorný
602d04dacf
build: drop allegedly no longer intrusive syslog-tests opt-in switch
The intention behind enabling the syslog tests on demand (642f74d) was
to tread cautiously as libtool used to alert that something controversial
is going on:

$ ./autogen.sh && ./configure --enable-syslog-tests && make check
> ...
> *** Warning: Linking the executable log.test against the loadable module
> *** _syslog_override.so is not portable!
> ...

owing to the fact that _syslog_override.so is being linked (through
libtool) with LDFLAGS=-module.

In fact, the issue did go away with a fix to the "make install" process
(ebcff54) for which _syslog_override.so* files were being picked
undesirably prior to the fix, which manifested itself also on
"make rpm" (hence the respective commit message):

> RPM build errors:
>     Installed (but unpackaged) file(s) found:
>    /usr/lib64/_syslog_override.so
>    /usr/lib64/_syslog_override.so.0
>    /usr/lib64/_syslog_override.so.0.0.0

The explanation for the secondary positive effect on the mentioned
libtool's warning going away is rather simple:
"lib" prefix switches the expectations about the result as a library
(also due to "libdir" destination) rather than as an executable object.
Change like that ensures the result is what libtool's manual calls
"dynamic module that can be opened by lt_dlopen" rather than "runtime
library that cannot" in the context of Mac OS X, which was likely the
cause of the portability warning.
(https://www.gnu.org/software/libtool/manual/libtool.html#FOOT10)

And because the tests themselves are run through the libtool wrapper
scripts capable of relinking and other magic

What libtool manual has to say on this topic:
> Note that libtool modules don’t need to have a "lib" prefix. However,
> Automake 1.4 or higher is required to build such modules.
referring to version released 15+ years back, which is assumed anyway.
(https://www.gnu.org/software/libtool/manual/libtool.html#Modules-for-libltdl)

* * *

That being said, there's now no longer a reason to have "syslog-tests"
enablement conditionalized (no longer supposed intrusive), hence enable
them unconditionally along the other libcheck-based tests
-- whenever libcheck is detected.

This changeset also drops any trace of --enable-syslog-tests switch
in various places using that (spec file, Travis CI configuration)
making for a complete vaporizatio of this choice.
2016-12-12 12:18:37 +01:00
Ferenc Wágner
8fad8acb67 configure: restrict nsl lib to where it's actually needed 2016-12-12 11:47:21 +01:00
Ferenc Wágner
1a9b4128e4 configure: restrict socket lib to where it's actually needed 2016-12-12 11:47:21 +01:00
Ferenc Wágner
cb5ee921c0 configure: restrict pthreads to where it's actually needed
mq_open() is no longer relevant beyond 70a9623 (Remove message queues).
2016-12-12 11:46:13 +01:00
Ferenc Wágner
56754d0509 configure: restrict -ldl to where it's actually needed
This reduces overlinking of qb-blackbox.  Being a seldom used executable,
the gains are mostly theoretical, but at least this silences warnings
from some QA tools.
2016-12-08 14:14:57 +01:00
Ferenc Wágner
64371306a6 configure: LTLIBOBJS is also a Make variable
So let's use the more friendly syntax.
2016-12-08 14:14:57 +01:00
Ferenc Wágner
ff7a3f4047 configure: help string cleanup
The [ default="no" ] branches were sense- and (mostly) harmless.
2016-12-08 14:11:28 +01:00
Ferenc Wágner
27d9b3b829 Fix typos: synchonization -> synchronization, paramaters -> parameters 2016-12-08 14:10:55 +01:00
Chrissie Caulfield
52db4997d7 Merge pull request #238 from jnpkrn/travis-issue-234
Continue with investigation of intermittent failures in Travis CI (#234)
2016-11-29 14:20:34 +00:00
Jan Pokorný
6fad6b7b2d
tests: better diagnose test_max_dgram_size test failures 2016-11-28 15:16:04 +01:00
Jan Pokorný
f321df7498
CI: travis: fix dh -> du + add "lsblk -f" diagnostics 2016-11-28 15:14:27 +01:00
Chrissie Caulfield
d03b718021 Merge pull request #235 from jnpkrn/travis-issue-234
CI: make travis watch for the issue #234
2016-11-24 15:18:15 +00:00
Jan Pokorný
e702848b0d
CI: make travis watch for the issue #234 2016-11-24 16:05:25 +01:00
Christine Caulfield
0a329683a7 version: Update version for 1.0.1 release 2016-11-24 09:44:27 +00:00
Chrissie Caulfield
3ca533b44e Merge pull request #233 from jnpkrn/copr-tito
build: git -> automatic COPR builds integration followup
2016-11-24 09:02:29 +00:00
Jan Pokorný
77cb88d6f8
build: tito: minor adjustments 2016-11-23 13:05:28 +01:00
Jan Pokorný
704c196426
doc: README: add a status badge+link for the COPR builds 2016-11-22 16:36:26 +01:00
Chrissie Caulfield
45712eae1b Merge pull request #232 from jnpkrn/copr-tito
build: allow for git -> automatic COPR builds integration
2016-11-22 14:27:43 +00:00
Jan Pokorný
27d51a8339
build: allow for git -> automatic COPR builds integration
New tito-related files makes the repository compatible with tito method
of building in COPR (https://fedorahosted.org/copr/wiki/UserDocs#Tito).
2016-11-18 23:32:44 +01:00
Jan Pokorný
85b82bd615
build: ensure debug make flags are not derived when unsuitable
Unfortunately, debug messages of GNU make are emitted to stdout, which
spoils the extracted output we rely on.  So prevent it (as well as
any other extraneous option) by force.

(discovered during git -> automatic COPR builds integration)
2016-11-18 23:29:07 +01:00
Jan Pokorný
f6e40425dc
build: ensure check_SCRIPTS are distributed
(discovered during git -> automatic COPR builds integration)
2016-11-18 22:41:14 +01:00
Chrissie Caulfield
b67f8ff9f4 Merge pull request #231 from jnpkrn/unlink-or-truncate
Unlink or truncate (as a fallback) files when shm IPC client terminates connection forcibly
2016-11-08 11:01:26 +00:00
Jan Pokorný
f610b1b161
tests: start stdlib failures injection effort with unlink{,at} + test
There are not many ways to test alternate code paths having failure of
some function from standard library as a precondition.

For a starter, we need to test failing unlink{,at} functions in a
controlled manner to mimic client and server path of the IPC connection
having different privileges to validate the previous commit.  But the
test suite cannot assume it has root privileges (so as to add artificial
user system-wide, which is a pretty stupid idea on its own), cannot
generally use stuff like chroot/namespacing (not to speak about
synergies of the former like docker).  So what's left is to make our
own playground, or better yet, use existing playground but just to
modify the rules of the game a bit when it's desired -- a variation
of old good LD_PRELOAD trick.

Note that this concept was already used in syslog tests (see commit
642f74d) and is now further extended using dlsym(RTLD_NEXT, "symbol")
to resolve the standard library symbol being shadowed by our little
"module".  This hence yields a customized wrapping we use to either
inject a call failure or to increase an invocation counter so as to
assure something has indeed been called.  As the mechanisms used are
not supposed to be available everywhere, the build system is
conditionalized respectively.

Back to our test when unlink{,at} fails, with the help of the described
mechanism, it was actually easy to massage test_ipc_server_fail_shm
into test_ipcc_truncate_when_unlink_fails_shm desired addition, which
is also featured in this commit, together with a modification to
resources.test script so that it expects particular number of empty
file leftovers (see previous commit).

It's expected that the module for failure injections will keep growing
so as to enable better overall coverage of the code (on the platforms
where this provision is available).
2016-11-04 19:06:08 +01:00
Jan Pokorný
1559192234
Med: rb: use new qb_rb_close_helper able to resort to file truncating
This changeset builds on previous 2-3 commits and represents the main
libqb's answer to the original question behind pacemaker's security
defect known as CVE-2016-7035.

Beside the helper partly unifying handling of qb_rb_force_close and
qb_rb_close, it provides the former with ability to use file truncating
as a fallback for when unlinking fails, e.g., because client (note that
mentioned is currently only relevant for the client side as normally
server is responsible for the lifecycle of the materialized files,
unless it crashes and only client is left to do its best) is not the
owner while they are placed at a directory with restricted deletion,
which enforces this very ownership condition.

In practice, this means that, at worst, just the zero-size files are
left behind, so not that much space exhaustion (usually "ramdisk"
like tmpfs is what backs default storage directory /dev/shm, so it
boils down to physical memory exhaustion, even if it can be just
for page cache and related overhead) can happen even on repeated
crashes as the memory mappings are cleared as much as possible.

Also openat/unlinkat functions (sported in qb_sys_unlink_or_truncate_at
as of the previous commit) are, when applicable, used so as to limit
possible race conditions between/during individual path traversals
(both files being got rid of presumably share the same directory).

Few words on which actions are attempted in which order for the
equivalent of qb_rb_force_close now:
There are subtle interactions between what's externally visible
(files) and what's not (memory mappings associated with such files),
and perhaps between memory pages management from the perspective of
the former (usually "ramdisk"/tmpfs) and the latter (mmap + munmap).
If the associated file is no longer publicly exposed by the means of
unlink (even if the object survives internally as refcounting is in
the game, with mmap holding a reference), memory mapping is not
affected.  On the other hand, if it's just limited by truncation
to zero size, memory mapping is aware and generates SIGBUS in response
to accessing respective addresses.  Similarly, accessing munmap'd
(no refcounting here) memory generates SIGSEGV.  For delicacy,
the inputs for all of unlink, truncate, and munmap are stored
at the mmap'd location we are about to drop, but that's just a matter
of making copies ahead of time.
At Ken's suggestion, the scheme is: (unlink or truncate) then munmap,
which has a benefit that externally visible (and program's life span
otherwise surviving!) part is eliminated first, with memory mappings
(disposed at program termination automatically at latest) to follow.
(There was originally a paranoid expectation on my side that truncate
on tmpfs actually does silent munmap, so that our munmap could in fact
tear down the mapping added in the interim by the libraries, signal
handler or due to requirements of another thread, also because of
munmap on the range without any current mappings will not fail, and
thus there's likely no portable way to non-intrusively check the
status, but also due to documented SIGBUS vs. SIGSEGV differences
the whole assumption appears bogus on the second thought.)

Relevant unit tests that exercise client-side unlinking:
- check_ipc: test_ipc_server_fail_shm, test_ipc_exit_shm
- new test in a subsequent commit
2016-11-04 19:05:35 +01:00
Jan Pokorný
7286215ec7
Low: unix: new qb_sys_unlink_or_truncate{,_at} helpers
These are intended for subsequent qb_rb_{force_,}close refactorization
and utilization of this new truncate as a fallback after unlink failure
as detailed in the commit to follow.

For newer POSIX revision compliant systems, there's "at" variant using
openat/unlinkat functions so that paths do not have to be traversed
in full anew when not needed (as both unlink and truncate operate on
the same path).
2016-11-04 19:02:50 +01:00
Jan Pokorný
189ca28db9
Med: rb: make it more robust against trivial IPC API misuses
...using a new private inline helper that is intended to "decorate"
argument (plus extra reference level added) to qb_rb_{force_,}close().
It is purposefully not hardwired to neither qb_rb_close (it's a public
API function that should not change its semantics) nor qb_rb_force_close
(just for symmetry, preempting issues when the two would differ, and
also makes them more mutually compatible, which is already expected
at qb_ipcc_shm_disconnect).

It sets the original ringbuffer pointer to NULL (having the immediate
impact on other threads/asynchronous handling) and also sets the
(currently underused) reference counter set to exacly 1 (that is
subsequently going to be decremented in qb_rb_close so that it's
sound in the current arrangement).

More in the comment at the helper.
Suitable places are also made to use it right away.
2016-11-04 19:02:32 +01:00
Jan Pokorný
c5aaea9207
Refactor: ipc_shm: better grip on ringbuffers to close
Also remove unused comment-introduced section of code.
2016-11-04 19:02:00 +01:00
Chrissie Caulfield
026aaa7bde Merge pull request #230 from jnpkrn/log_thread
Med: log_thread: logt_wthread_lock is vital for logging thread
2016-10-21 15:43:17 +01:00
Chrissie Caulfield
7e5212b6a3 Merge pull request #228 from jnpkrn/maint
Various cleanups (symbol imports, typos, doc)
2016-10-21 15:31:13 +01:00
Jan Pokorný
ca710b2505
Refactor: log_thread: fix and diminish inferior comments 2016-10-21 09:42:48 +02:00
Jan Pokorný
07542cf693
Med: log_thread: logt_wthread_lock is vital for logging thread
This fixes issue with would-fail-if-applied-to-thread-right-away
qb_log_thread_priority_set invocation when logging thread doesn't
exist yet, which will arrange for calling itself at the time of
thread's birth that is the moment it will actually fail.
In this + lock-could-not-have-been-initialized corner cases, the
already running thread would proceed as allowed by error condition
handling in the main thread, trying to dereference uninitialized
(or outdated) pointer to the lock at hand, resulting in segfault.

Also include the test that would have been caught that (we use the
fact that it doesn't matter whether setting of the scheduler parameters
fails due to bad input or just because of lack of privileges as it's
the failure at the right moment that is of our interest).

See also:
https://github.com/ClusterLabs/libqb/issues/229
2016-10-21 09:42:16 +02:00
Christine Caulfield
e13d15e993 tests: Unit test for previous zero tag patch
Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
2016-10-20 14:52:52 +01:00
Christine Caulfield
0d6a698931 log: Remove check for HAVE_SCHED_GET_PRIORITY_MAX
it doesn't exist

Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
2016-10-20 09:50:12 +01:00
Jan Pokorný
0d90bcb0ad
tests: SIGSTOP cannot be caught, blocked, or ignored
...per signal(7), so it is foolish trying to do so.
2016-10-19 00:40:56 +02:00