Commit graph

64 commits

Author SHA1 Message Date
Mark Stapp e13a4485bf libs: return from change_caps if no caps
When called without caps/privs, just return from "change_caps"
instead of exiting - it's possible that a process may not need
privs, but a lib (for example) may use the api.

Signed-off-by: Mark Stapp <mjs@cisco.com>
2025-01-31 13:13:48 -05:00
Martin Buck 4ed9147d98 lib: Fix privs syscaps (pset_t) allocation
Don't over-allocate syscaps in zcaps2sys(): This is just a single struct
(pset_t) with a count and a pointer to an array of capabilities, not an
array. So only allocate a single pset_t, not num copies of it.

The allocation size of syscaps->caps then needs to be based on the number of
Linux capabilities (count), but that is already handled properly a few lines
below.

Note that this fix is mostly cosmetic and for correctness. There was no
potential for memory corruption, because num is guaranteed to be nonzero. So
at least the one required pset_t was always allocated (but potentially much
more).

Signed-off-by: Martin Buck <mb-tmp-tvguho.pbz@gromit.dyndns.org>
2025-01-08 10:38:56 +01:00
Donald Sharp b6f1a4089f lib: zebra.h uses pwd.h when it should not
Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2024-01-09 12:50:40 -05:00
Donald Sharp 88f8c90073 lib, vtysh: Remove inclusion of grp.h for everyone
Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2024-01-04 14:34:41 -05:00
Donald Sharp 78690deaf2 lib: Move some priviledge headers to file they are used.
Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2024-01-04 14:04:36 -05:00
David Lamparter acddc0ed3c *: auto-convert to SPDX License IDs
Done with a combination of regex'ing and banging my head against a wall.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2023-02-09 14:09:11 +01:00
Rafael Zalamena eb23e17fc7 lib: fix capability double resource allocation
Don't let `zprivs_caps_init` allocate resources without checking if
there were other caps previously allocated.

This fixes a memory leak that happens on daemons that `fork()` and reuse
the `<daemon>_di` (see `ldpd`/`lde`/`ldpe` code).

Signed-off-by: Rafael Zalamena <rzalamena@opensourcerouting.org>
2022-12-12 10:39:17 -03:00
Donald Sharp cb1991af8c *: frr_with_mutex change to follow our standard
convert:
	frr_with_mutex(..)

to:
	frr_with_mutex (..)

To make all our code agree with what clang-format is going to produce

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2022-07-20 15:50:32 -04:00
Donald Sharp 88be4045c9 lib: Allow downgrade of all caps when none are specified
Staticd when run tells privs.c that it does not need any
priviledges.  The lib/privs.c code was not downgrading
any and all permissions it may have been given at startup.

Since we don't need any let's actually tell the system that
FRR does not need the capabilities anymore in the case
where a daemon does not ask for any cap's.

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2022-06-29 14:40:57 -04:00
Anuradha Karuppiah 92b06d6bdd lib: add SYS_RAWIO to the capabilities definitions
Signed-off-by: Anuradha Karuppiah <anuradhak@nvidia.com>
2022-06-27 07:56:55 -04:00
Donald Sharp 485ac9a79b *: Fix spelling of neccessary
Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2022-04-19 08:11:29 -04:00
Donald Sharp 575a25974b lib: Add ZCAP_IPC_LOCK
We'll need ZCAP_IPC_LOCK for future work coming down the pike
related to dataplane work being done.

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2021-05-11 19:54:40 -04:00
David Lamparter bf8d3d6aca *: require semicolon after DEFINE_MTYPE & co
Back when I put this together in 2015, ISO C11 was still reasonably new
and we couldn't require it just yet.  Without ISO C11, there is no
"good" way (only bad hacks) to require a semicolon after a macro that
ends with a function definition.  And if you added one anyway, you'd get
"spurious semicolon" warnings on some compilers...

With C11, `_Static_assert()` at the end of a macro will make it so that
the semicolon is properly required, consumed, and not warned about.

Consistently requiring semicolons after "file-level" macros matches
Linux kernel coding style and helps some editors against mis-syntax'ing
these macros.

Signed-off-by: David Lamparter <equinox@diac24.net>
2021-03-17 06:18:17 +01:00
David Lamparter 877057b67a lib: have a lib_privs for ... lib privs
Logging code might want to raise privs for file operations.

Signed-off-by: David Lamparter <equinox@diac24.net>
2021-02-12 19:28:52 +01:00
Donald Sharp cae8bc967c *: Remove solaris from FRR
The Solaris code has gone through a deprecation cycle.  No-one
has said anything to us and worse of all we don't have any test
systems running Solaris to know if we are making changes that
are breaking on Solaris.  Remove it from the system so
we can clean up a bit.

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2020-09-21 10:02:20 -04:00
Quentin Young e117b7c528 lib: explicitly cast -1 to uid_t
We should be explicit about what's happening here

Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-09-02 16:54:38 -04:00
David Lamparter 3efd0893d0 *: un-split strings across lines
Remove mid-string line breaks, cf. workflow doc:

  .. [#tool_style_conflicts] For example, lines over 80 characters are allowed
     for text strings to make it possible to search the code for them: please
     see `Linux kernel style (breaking long lines and strings)
     <https://www.kernel.org/doc/html/v4.10/process/coding-style.html#breaking-long-lines-and-strings>`_
     and `Issue #1794 <https://github.com/FRRouting/frr/issues/1794>`_.

Scripted commit, idempotent to running:
```
python3 tools/stringmangle.py --unwrap `git ls-files | egrep '\.[ch]$'`
```

Signed-off-by: David Lamparter <equinox@diac24.net>
2020-07-14 10:37:25 +02:00
Mark Stapp d617669d37 lib: don't try to change/reset capabilities if process has none
A couple of daemons take/use no capabilities/privs; allow cleanup
of the privs/capabilities library module even if a daemon has no
caps.

Signed-off-by: Mark Stapp <mjs@voltanet.io>
2020-06-11 15:31:54 -04:00
David Lamparter 00dffa8cde lib: add frr_with_mutex() block-wrapper
frr_with_mutex(...) { ... } locks and automatically unlocks the listed
mutex(es) when the block is exited.  This adds a bit of safety against
forgetting the unlock in error paths & co. and makes the code a slight
bit more readable.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2019-09-03 17:15:17 +02:00
Lou Berger 31e944a8a7
Merge pull request #3045 from opensourcerouting/atoms
READY: lists/skiplists/rb-trees new API & sequence lock & atomic lists
2019-04-30 10:26:35 -04:00
Mark Stapp 8875d0515e libs: control privs changes with refcount
Use a refcount to control privs changes. Support process-wide
privs apis, as well as per-pthread apis.

Signed-off-by: Mark Stapp <mjs@voltanet.io>
2019-04-22 09:32:41 -04:00
David Lamparter 7e3a1ec742 lib: ZEBRA_NUM_OF -> array_size
The latter is widely used, e.g. in the Linux kernel.

Signed-off-by: David Lamparter <equinox@diac24.net>
2019-04-18 12:44:29 +02:00
Mark Stapp 064e2f3280 libs: fix race in privs changes
Use the privs struct mutex more strictly, to ensure that the
privs are at the level the caller expects when the apis
return.

Signed-off-by: Mark Stapp <mjs@voltanet.io>
2019-03-17 13:25:16 -04:00
Mark Stapp c5c44d4b41 libs: make privilege escalation thread-safe
Privs escalation is process-wide, and a multi-threaded process
can deadlock. This adds a mutex and a counter to the privs
object, preventing multiple threads from making the privs
escalation system call.

Signed-off-by: Mark Stapp <mjs@voltanet.io>
2019-03-05 11:08:31 -05:00
Quentin Young 72de5f4b5d lib: fix garbage array size in zprivs_init
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2019-02-15 16:27:55 +00:00
Quentin Young 1c77d034e5 lib: fix uninitialized value use in privs.c
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2019-02-14 19:01:33 +00:00
Quentin Young 450971aa99 *: LIB_[ERR|WARN] -> EC_LIB
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-09-13 19:34:28 +00:00
Donald Sharp ff245f0e02 lib: Convert privs.c to use new error-code subsystem
Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2018-09-06 20:50:58 +00:00
David Lamparter 6017c3a2e7 lib: add frr_elevate_privs() wrapper
Used as:
  frr_elevate_privs(&my_privs) {
    ... code ...
  }
and handles privilege raise/lower automatically in conjunction with the
C expression block.  This makes it impossible to accidentally exit a
function with privileges raised (and then running a whole bunch of other
code with privs.)

Signed-off-by: David Lamparter <equinox@diac24.net>
2018-08-14 20:02:05 +00:00
Donald Sharp 8e04538cae lib: Fix privs when using HAVE_CAPABILITIES
If your daemon does not need any special privileges
and you are compiling with HAVE_CAPABILIES, the
zprivs->change pointer will end up NULL due
to the way zprivs_caps_init.  So as a check
let's add a NULL check for zprivs->change
and set it to a function that will do nothing.

This change prevents a crash if you raise privileges
when your daemon needs no special privileges.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2018-06-21 11:22:51 -04:00
David Lamparter 4093d47b99 lib: terminate capabilities only if initialized
zprivs_caps_init() is called conditionally, apply the same condition on
terminate.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-08-21 21:53:46 +02:00
David Lamparter 37a1f2fbb6 lib: privs: make uid/gid accessible before setuid
This splits off privs_preinit(), which does the lookups for user and
group IDs.  This is so the init code can create state directories while
still running as root.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-08-02 23:36:39 +02:00
David Lamparter 9d303b37d7 Revert "*: reindent pt. 2"
This reverts commit c14777c6bf.

clang 5 is not widely available enough for people to indent with.  This
is particularly problematic when rebasing/adjusting branches.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-07-22 14:52:33 +02:00
whitespace / reindent c14777c6bf
*: reindent pt. 2
w/ clang 5

* reflow comments
* struct members go 1 per line
* binpack algo was adjusted
2017-07-17 15:26:02 -04:00
whitespace / reindent d62a17aede *: reindent
indent.py `git ls-files | pcregrep '\.[ch]$' | pcregrep -v '^(ldpd|babeld|nhrpd)/'`

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-07-17 14:04:07 +02:00
Donald Sharp 9e962de4b6 Merge remote-tracking branch 'origin/stable/3.0' 2017-05-24 11:22:35 -04:00
Quentin Young fb12f2c75f lib: fix array sizes for capability map
incorrect array sizes causing out of bounds read and potentially
incorrect capability settings

introduced in 1b322039

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-05-22 02:32:12 +00:00
David Lamparter 896014f4bc *: make consistent & update GPLv2 file headers
The FSF's address changed, and we had a mixture of comment styles for
the GPL file header.  (The style with * at the beginning won out with
580 to 141 in existing files.)

Note: I've intentionally left intact other "variations" of the copyright
header, e.g. whether it says "Zebra", "Quagga", "FRR", or nothing.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-05-15 16:37:41 +02:00
Donald Sharp de72643d15 Merge pull request #315 from LabNConsulting/working/master/patch/bgp-startup
Restore functionality broken/overridden by 857b544649
2017-04-18 12:14:58 -04:00
Lou Berger c888ea1647 lib/frr: fix reference to zsuid before it is set
Signed-off-by: Lou Berger <lberger@labn.net>
2017-04-02 16:18:53 -04:00
Mike Tancsa 7e5b539722 lib: fix usage of getgrouplist() in *BSD
On BSD systems, the getgrouplist() function returns 0 if successful and
-1 on error.

Linux in the other hand returns *ngroups (the number of groups of which
user is a member) on success and -1 on error.

Given this difference, the most portable way to use getgrouplist()
is use its return value only for checking if it succeeded or not.

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
2017-04-01 00:50:40 -04:00
Martin Winter 38657f2ebd lib: Don't change uid/gid if we are already the correct uid/gid
Signed-off-by: Martin Winter <mwinter@opensourcerouting.org>
2017-01-26 00:45:05 +07:00
David Lamparter e227e24231 lib: privs: always look up VTY group
Even if we're running without user switch, we should still try to honor
the VTY group.  This applies both to watchquagga (which always runs as
root) as well as "no-userswitch" configurations for other daemons.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2016-11-09 13:40:37 +01:00
Renato Westphal eac6e3f027 ldpd: adapt the code for Quagga
Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
2016-09-23 09:31:09 -04:00
David Lamparter 4a1ab8e405 *: split & distribute memtypes and stop (re|ab)using lib/ MTYPEs
This is a rather large mechanical commit that splits up the memory types
defined in lib/memtypes.c and distributes them into *_memory.[ch] files
in the individual daemons.

The zebra change is slightly annoying because there is no nice place to
put the #include "zebra_memory.h" statement.

bgpd, ospf6d, isisd and some tests were reusing MTYPEs defined in the
library for its own use.  This is bad practice and would break when the
memtype are made static.

Acked-by: Vincent JARDIN <vincent.jardin@6wind.com>
Acked-by: Donald Sharp <sharpd@cumulusnetworks.com>
[CF: rebased for cmaster-next]
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Signed-off-by: Christian Franke <chris@opensourcerouting.org>
2016-09-19 16:31:04 -04:00
Lou Berger 7c101d199e lib: fix memory leak in zprivs_caps_init 2016-09-03 11:05:51 -04:00
Donald Sharp b8b341d7c3 lib: Fix priviledge modification for vty group specified
When attempting to switch runtime permissions over to
the correct group specified for the vty group, if the
user specified to run as does not have that vty group
then do warn about the issue and stop running

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
Reported-by: Thomas Martin <tmartincpp@gmail.com>
2016-04-08 20:33:15 -04:00
David Lamparter 4a9ea50e1b lib: add getgrouplist() for Solaris
Of course Solaris doesn't have getgrouplist()...

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2016-04-08 20:33:15 -04:00
Timo Teräs 6d75781681 privs: fix privilege dropping to use system defined groups
It may be requred for quagga process to belong to additional
groups. E.g. nhrp module will need to talk to strongSwan using
vici and may require additional permissions. Initialize groups
from the system group database.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2016-04-08 20:32:02 -04:00
Christian Franke 1b32203911 lib/privs: Don't use CAP_NET_BROADCAST
From what I can tell, CAP_NET_BROADCAST has never been required for any
functionality in the Linux kernel, so we do not really need it.

However, it causes breakage in contexts where Quagga is started with a
limited set of capabilities, e.g. in Docker, because these may not
include CAP_NET_BROADCAST and in the case of Docker do not even support
adding CAP_NET_BROADCAST.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2016-04-08 20:32:02 -04:00