Commit Graph

102 Commits

Author SHA1 Message Date
Ken Gaillot
1070cbc17c
Make some logs less noisy (#495)
* log: lower IPC connection issues to info level

... in handle_new_connection(). The caller has better context for whether a
problem merits a warning or error, and the function's return code is
sufficiently descriptive to do so. Some problems may be expected or able to be
worked around.

For example, Pacemaker's crm_mon attempts to contact pacemakerd IPC. On a
Pacemaker Remote node, that IPC will be unavailable, and crm_mon can check the
libqb return code to detect and handle that situation gracefully.

* log: lower some ringbuffer debug messages to trace level

They're rather noisy, with every shm-based IPC connection generating multiple
obscure messages like:

  debug: shm size:1048589; real_size:1052672; rb->word_size:263168

and every disconnect generating the rather unhelpful:

  debug: qb_ipcc_disconnect()

along with multiple messages like:

  debug: Closing ringbuffer: /dev/shm/qb-10986-11014-34-26VRvs/qb-request-cmap-header

All of these seem appropriate to trace level.
2024-01-31 09:44:16 +00:00
Chrissie Caulfield
06e318fdc0
blackbox: Sanitize items read from the blackbox header (#438)
covscan complained we don't check the blackbox header when
reading it in. (quite reasonably)

Note that we still get a covscan error for ->shared_data, but that's
really impossible to verify in the read routine, so I'll leave the
covscan waiver to handle that.
2022-03-18 10:04:58 +00:00
Chrissie Caulfield
bdc716036a
Some bugs spotted by coverity (#399) 2020-05-28 07:30:26 +01:00
Jan Pokorný
484fddddb8 ringbuffer: fix mistaken errno handling around _rb_chunk_reclaim
Previously, there were two separate logical issues:

- errno could be set negative in qb_rb_chunk_alloc when
  when "reclaim" notifier failed

- _rb_chunk_reclaim (note: local scoped, hence comfortable for changes)
  was already setting errno at a single (coincidentally, in a correct
  way, but that'd be overwritten with the inverse because of the
  previous logical issue in qb_rb_chunk_alloc), so make it set errno
  at each failure path (now also when internal integrity in
  _rb_chunk_reclaim failed(), sparing the callers to double on that task

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
2019-07-26 09:39:58 +01:00
Christine Caulfield
6a4067c1d1 ipc: Use mkdtemp for more secure IPC files
Use mkdtemp makes sure that IPC files are only visible to the
owning (client) process and do not use predictable names outside
of that.

This is not meant to be the last word on the subject, it's mainly a
simple way of making the current libqb more secure. Importantly, it's
backwards compatible with an old server.

It calls rmdir on the directory created by mkdtemp way too often, but
it seems to be the only way to be sure that things get cleaned up on
the various types of server/client exit. I'm sure we can come up with
something tidier for master but I hope this, or something similar, will
be OK for 1.0.x.
2019-04-08 16:24:19 +01:00
Christine Caulfield
e322e98dc2 ipc: use O_EXCL on SHM files, and randomize the names
Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
2019-04-08 13:18:34 +01:00
Jan Pokorný
ef5b1cf4cb
warnings cleanup: Wformat: sign-correct PRIu32 specifiers as appropriate
Looks like these are not accepted with splint checker.  Also fix some
other minor type -- print format specifier discrepancies.

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
2017-12-21 01:50:15 +01: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
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
Helge Deller
85082aa059 Fix alignment issues on hppa
libqb fails to build on the hppa architecture, because the built-in
testcases fail as can be seen here:
http://buildd.debian-ports.org/status/fetch.php?pkg=libqb&arch=hppa&ver=0.17.0-2&stamp=1409458262

I did analyzed why they fail, and the reason is that on hppa we have
somewhat more complicated requirements (e.g. alignments) which needs to
be followed in order to mmap shared pages between processes. It's
different than what can be done compared to ia64 and sparc.
The attached patch fixes libqb on the hppa architecture and with it all
testcases finish successful.

By the way, I fixed a small typo in configure.ac too where arm platforms
prints "ia64"...

Forwarded-From: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=760199
Signed-off-by: Christoph Berg <myon@debian.org>
2015-04-24 12:26:01 +02:00
Michael Chapman
fad9ce01a0 ringbuffer: fix size in qb_rb_create_from_file()
qb_rb_open() expects the size field to represent the maximum chunk size.
It adds QB_RB_CHUNK_MARGIN + 1 and rounds up to the page size to
determine the ringbuffer's total size. When creating a ringbuffer from a
file we must compensate by subtracting this amount from the file's size.
2014-06-05 14:36:56 +10:00
Michael Chapman
edd2eec16c ringbuffer: fix qb_rb_open_2() debug format string
qb_log_target_formats() does not support formatting size_t values with
%zd.  Use %ld to format them as long integers instead.
2014-06-05 14:36:43 +10:00
David Vossel
00082df49f Low: blackbox: Abort blackbox logging on ringbuffer overwrite reclaim error 2013-10-08 21:59:59 -05:00
David Vossel
47c690dbbc Low: ringbuffer: Abort during chunk reclaim if OVERWRITE flag is set and reclaim fails. 2013-10-03 20:40:12 -05:00
David Vossel
9102c8d956 Fixes double fd close 2013-07-22 16:54:52 -05:00
David Vossel
bde8496352 High: ringbuffer: Make max_size of ringbuffer accurate so shm ipc max msg size value is honored 2013-07-18 22:01:29 -05:00
David Vossel
a2bdeed3cc Fix: ringbuffer: Add file header version field and detect reading corrupted blackbox files using hash value
In the future, if something changes in the file header we can
increment the version and split up the parsing into separate functions
for backwards compatibility.
2013-06-27 19:32:05 -05:00
Angus Salkeld
67dc29f48d Use the new atomic ops in the ringbuffer
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2013-05-13 10:15:55 +10:00
Jeremy Fitzhardinge
9b3be0b450 ringbuffer: use atomic ops on ringbuffer chunk magic
The ringbuffer protocol uses the chunk magic number to indicate to the
other side what state a chunk is in.  It's therefore important to use
strongly ordered memory writes to make sure that neither the compiler
nor the CPU change the apparent order of the writes, since that would
result in corrupted messages.

Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2013-04-24 11:57:49 +10:00
Andrew Beekhof
f16dca6df9 Indicate when/why qb_rb_force_close() fails to remove share memory files 2013-04-11 13:00:12 +10:00
Angus Salkeld
7667536626 Deal better with corrupt blackbox files.
fixes #59
Thanks to Jan Friesse for the reproducer.
https://github.com/jfriesse/csts/blob/master/tests/fplay-segfault.sh

Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2013-04-02 13:52:57 +11:00
Angus Salkeld
6ba054713e RB: make the "sem" abstraction into a notifier
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2013-02-18 23:25:10 +11:00
Angus Salkeld
aedcb97690 Make sure atomic's are initialized (for non-gcc atomic).
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-09-11 14:42:04 +10:00
Angus Salkeld
d92bfa2b0e openbsd doesn't have EBADMSG
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-08-29 22:30:58 +10:00
Jeremy Fitzhardinge
4a1e24ee3e Don't free rb->shared_hdr in qb_rb_create_from_file()
Since qb_rb_close() frees it by munmapping it.
2012-08-23 10:14:11 -07:00
Angus Salkeld
e5be0396a7 RB: set the new read pointer after clearing the header.
This is to prevent a situation where a fast writer will
write their new chunk between setting the new read pointer
and clearing the header.

Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-07-17 21:18:47 +10:00
Angus Salkeld
3feb3b6b8f RB: improve the debug print outs
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-07-17 20:45:29 +10:00
Angus Salkeld
b9a992a5d3 RB: be more explicit about the word alignment
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-07-17 19:37:52 +10:00
Angus Salkeld
15d0291a8a RB: cleanup the macros for wrapping the index
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-07-17 12:38:22 +10:00
Angus Salkeld
6e1d9054c3 RB: use sem_getvalue as a tie breaker when read_pt == write_pt
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-07-17 12:38:22 +10:00
Angus Salkeld
bdc63cdf1a RB: if read or peek don't get the message then re-post to the semaphore
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-07-17 12:38:22 +10:00
Angus Salkeld
121abe3dbe RB: use internal reclaim function
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-07-17 12:22:26 +10:00
Angus Salkeld
22569f51ba Add user control of the permissions that are set on the shared mem files
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-06-13 08:21:09 +10:00
Angus Salkeld
3369581066 RB: use the same mechanism in reclaim as read/peek to detect end-of-ring
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-06-07 12:02:32 +10:00
Angus Salkeld
85b39906a9 RB: fix compiler warning.
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-05-28 19:10:13 +10:00
Andrew Beekhof
77fa3650dd RB: Read the file size into an initialized variable of the correct size 2012-05-28 12:25:10 +10:00
Angus Salkeld
e0bc3a15ce RB: to be safer save the read and write pointers at the top of the blackbox
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-05-25 14:01:19 +10:00
Angus Salkeld
a8405ab06d blackbox: fix the print_from_file()
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-05-24 19:22:55 +10:00
Angus Salkeld
4fdabe5bed RB: add an option to not use any semaphores
(brought over from the "speed" branch)

Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-05-24 19:16:59 +10:00
Angus Salkeld
3f4a175312 IPC: make it possible for a root client to talk to a non-root server.
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-04-30 08:25:57 +10:00
Angus Salkeld
2ae58d2472 RB: fix test failure on ppc
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-03-07 11:37:12 +11:00
Angus Salkeld
9027c3531b RB: change the name of the size to word_size to be more clear
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-03-07 10:34:19 +11:00
Angus Salkeld
2a0cdd8f5f RB: add a debug message if trying to read a message of the wrong size
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-02-14 16:43:50 +11:00
Angus Salkeld
2b2dfd5d2c RB: use the proper struct not the typedef in the implementation.
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-02-08 22:00:50 +11:00
Angus Salkeld
dff3ff4afe RB: Fix potential mem leak
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-02-08 22:00:50 +11:00
Angus Salkeld
7a6382f0b6 Use safer versions of string functions (strcpy -> strlcpy)
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-02-08 22:00:49 +11:00
Angus Salkeld
477fac4e01 IPC: fix resource cleanup if the server dies
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2012-01-12 15:09:58 +11:00
Angus Salkeld
ab3dc60f7f Merge some portability changes from the mingw branch
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2011-12-21 14:23:43 +11:00
Angus Salkeld
ef77398738 Fix errors found by api-sanity-autotest
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
2011-11-25 17:16:19 +11:00