Skip to content

Commit ce4e49e

Browse files
authored
Merge pull request #3364 from ruihe774/geteuid
chore: Use geteuid() instead of getuid() to check privilege
2 parents 655e467 + f9bf9ac commit ce4e49e

File tree

6 files changed

+24
-5
lines changed

6 files changed

+24
-5
lines changed

src/libostree/ostree-bootloader-zipl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ _ostree_bootloader_zipl_post_bls_sync (OstreeBootloader *bootloader, int bootver
432432
// This can happen in a unit testing environment; at some point what we want to do here
433433
// is move all of the zipl logic to a systemd unit instead that's keyed of
434434
// ostree-finalize-staged.service.
435-
if (getuid () != 0)
435+
if (!ot_util_process_privileged ())
436436
return TRUE;
437437

438438
// If we're in a booted deployment, we don't need to spawn a container.

src/libostree/ostree-repo-commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@ ostree_repo_prepare_transaction (OstreeRepo *self, gboolean *out_transaction_res
16581658
self->reserved_blocks = reserved_bytes / self->txn.blocksize;
16591659

16601660
/* Use the appropriate free block count if we're unprivileged */
1661-
guint64 bfree = (getuid () != 0 ? stvfsbuf.f_bavail : stvfsbuf.f_bfree);
1661+
guint64 bfree = (ot_util_process_privileged () ? stvfsbuf.f_bfree : stvfsbuf.f_bavail);
16621662
if (bfree > self->reserved_blocks)
16631663
self->txn.max_blocks = bfree - self->reserved_blocks;
16641664
else

src/libostree/ostree-sysroot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ ostree_sysroot_initialize_with_mount_namespace (OstreeSysroot *self, GCancellabl
285285
return FALSE;
286286

287287
/* Do nothing if we're not privileged */
288-
if (getuid () != 0)
288+
if (!ot_util_process_privileged ())
289289
return TRUE;
290290

291291
/* We also assume operating on non-booted roots won't have a readonly sysroot */

src/libotutil/ot-unix-utils.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#include <stdlib.h>
3333
#include <string.h>
3434
#include <sys/types.h>
35+
#include <sys/prctl.h>
36+
#include <linux/prctl.h>
37+
#include <linux/capability.h>
3538
#include <unistd.h>
3639

3740
/* Ensure that a pathname component @name does not contain the special Unix
@@ -102,3 +105,17 @@ ot_util_path_split_validate (const char *path, GPtrArray **out_components, GErro
102105
ot_transfer_out_value (out_components, &ret_components);
103106
return TRUE;
104107
}
108+
109+
/* Check if current process is privileged */
110+
gboolean
111+
ot_util_process_privileged (void)
112+
{
113+
if (geteuid() != 0)
114+
return FALSE;
115+
116+
// https://github.com/containers/bootc/blob/c88fcfd6e145863408bde7d4706937dd323f64e2/lib/src/cli.rs#L621
117+
if (prctl (PR_CAPBSET_READ, CAP_SYS_ADMIN) != 1)
118+
return FALSE;
119+
120+
return TRUE;
121+
}

src/libotutil/ot-unix-utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ gboolean ot_util_filename_validate (const char *name, GError **error);
3939

4040
gboolean ot_util_path_split_validate (const char *path, GPtrArray **out_components, GError **error);
4141

42+
gboolean ot_util_process_privileged (void);
43+
4244
G_END_DECLS

src/ostree/ot-main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ maybe_setup_mount_namespace (gboolean *out_ns, GError **error)
116116
*out_ns = FALSE;
117117

118118
/* If we're not root, then we almost certainly can't be remounting anything */
119-
if (getuid () != 0)
119+
if (!ot_util_process_privileged ())
120120
return TRUE;
121121

122122
/* If the system isn't booted via libostree, also nothing to do */
@@ -580,7 +580,7 @@ ostree_admin_sysroot_load (OstreeSysroot *sysroot, OstreeAdminBuiltinFlags flags
580580
/* Only require root if we're manipulating a booted sysroot. (Mostly
581581
* useful for the test suite)
582582
*/
583-
if (booted && getuid () != 0)
583+
if (booted && !ot_util_process_privileged ())
584584
{
585585
g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
586586
"You must be root to perform this command");

0 commit comments

Comments
 (0)