chore: Use geteuid() instead of getuid() to check privilege
authorMisaki Kasumi <misakikasumi@outlook.com>
Wed, 18 Dec 2024 15:48:41 +0000 (23:48 +0800)
committerMisaki Kasumi <misakikasumi@outlook.com>
Fri, 20 Dec 2024 11:13:59 +0000 (19:13 +0800)
src/libostree/ostree-bootloader-zipl.c
src/libostree/ostree-repo-commit.c
src/libostree/ostree-sysroot.c
src/libotutil/ot-unix-utils.c
src/libotutil/ot-unix-utils.h
src/ostree/ot-main.c

index 2804ed2644f94ac280f8d0adbd3ec438aa79e9c4..f0c18cbcfc134caa05aaf055035621f57df759b0 100644 (file)
@@ -432,7 +432,7 @@ _ostree_bootloader_zipl_post_bls_sync (OstreeBootloader *bootloader, int bootver
   // This can happen in a unit testing environment; at some point what we want to do here
   // is move all of the zipl logic to a systemd unit instead that's keyed of
   // ostree-finalize-staged.service.
-  if (getuid () != 0)
+  if (!ot_util_process_privileged ())
     return TRUE;
 
   // If we're in a booted deployment, we don't need to spawn a container.
index 17b8a97f4e690114e39e0ab461ccf35588ed5765..18b2562c8ae752b829725e418c171aa535aac95a 100644 (file)
@@ -1658,7 +1658,7 @@ ostree_repo_prepare_transaction (OstreeRepo *self, gboolean *out_transaction_res
   self->reserved_blocks = reserved_bytes / self->txn.blocksize;
 
   /* Use the appropriate free block count if we're unprivileged */
-  guint64 bfree = (getuid () != 0 ? stvfsbuf.f_bavail : stvfsbuf.f_bfree);
+  guint64 bfree = (ot_util_process_privileged () ? stvfsbuf.f_bfree : stvfsbuf.f_bavail);
   if (bfree > self->reserved_blocks)
     self->txn.max_blocks = bfree - self->reserved_blocks;
   else
index 925c66a7e3e2f5fba210bed21fa49a2768089a8a..3968c38fedea2f182577bc7fcfbe8108d99ffbfe 100644 (file)
@@ -285,7 +285,7 @@ ostree_sysroot_initialize_with_mount_namespace (OstreeSysroot *self, GCancellabl
     return FALSE;
 
   /* Do nothing if we're not privileged */
-  if (getuid () != 0)
+  if (!ot_util_process_privileged ())
     return TRUE;
 
   /* We also assume operating on non-booted roots won't have a readonly sysroot */
index 33cd1c029c3172a8032ba8ffe25dc713219e0a73..7a3192fe54eb3db77305ca12c6c1a89998d44975 100644 (file)
@@ -102,3 +102,10 @@ ot_util_path_split_validate (const char *path, GPtrArray **out_components, GErro
   ot_transfer_out_value (out_components, &ret_components);
   return TRUE;
 }
+
+/* Check if current process is privileged */
+gboolean
+ot_util_process_privileged (void)
+{
+  return geteuid() == 0;
+}
index 3e4be2f9ad0ad8a6c0fea18d4ac49501053027b2..38f73e498150cf81c3db1578cc0685debb399b00 100644 (file)
@@ -39,4 +39,6 @@ gboolean ot_util_filename_validate (const char *name, GError **error);
 
 gboolean ot_util_path_split_validate (const char *path, GPtrArray **out_components, GError **error);
 
+gboolean ot_util_process_privileged (void);
+
 G_END_DECLS
index fa4eb53f2aad1d270f4445fa23d14bb1f021abc8..d47a59cad59a715b123601f6b01990b30292db26 100644 (file)
@@ -116,7 +116,7 @@ maybe_setup_mount_namespace (gboolean *out_ns, GError **error)
   *out_ns = FALSE;
 
   /* If we're not root, then we almost certainly can't be remounting anything */
-  if (getuid () != 0)
+  if (!ot_util_process_privileged ())
     return TRUE;
 
   /* If the system isn't booted via libostree, also nothing to do */
@@ -580,7 +580,7 @@ ostree_admin_sysroot_load (OstreeSysroot *sysroot, OstreeAdminBuiltinFlags flags
       /* Only require root if we're manipulating a booted sysroot. (Mostly
        * useful for the test suite)
        */
-      if (booted && getuid () != 0)
+      if (booted && !ot_util_process_privileged ())
         {
           g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
                        "You must be root to perform this command");