Blob Blame History Raw
From b3f2d99ce6ab19b21a115decac0495d659153837 Mon Sep 17 00:00:00 2001
From: Jakub Filak <jfilak@redhat.com>
Date: Thu, 14 May 2015 10:11:36 +0200
Subject: [PATCH] lib: parse fsgid and don't fall back to 0

Let users of API to fall back to 0 on their own and provide them with a
function for getting fsgid from /proc/[pid]/status.

Thanks Florian Weimer <fweimer@redhat.com>

Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
 src/include/internal_libreport.h |  2 ++
 src/lib/get_cmdline.c            | 32 ++++++++++++++++++++++++--------
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/src/include/internal_libreport.h b/src/include/internal_libreport.h
index 66f91e9..25af681 100644
--- a/src/include/internal_libreport.h
+++ b/src/include/internal_libreport.h
@@ -660,6 +660,8 @@ char* get_cwd(pid_t pid);
 char* get_rootdir(pid_t pid);
 #define get_fsuid libreport_get_fsuid
 int get_fsuid(const char *proc_pid_status);
+#define get_fsgid libreport_get_fsgid
+int get_fsgid(const char *proc_pid_status);
 #define dump_fd_info_ext libreport_dump_fd_info_ext
 int dump_fd_info_ext(const char *dest_filename, const char *proc_pid_fd_path, uid_t uid, gid_t gid);
 #define dump_fd_info libreport_dump_fd_info
diff --git a/src/lib/get_cmdline.c b/src/lib/get_cmdline.c
index c55de30..5547877 100644
--- a/src/lib/get_cmdline.c
+++ b/src/lib/get_cmdline.c
@@ -188,21 +188,26 @@ char* get_rootdir(pid_t pid)
     return malloc_readlink(buf);
 }
 
-int get_fsuid(const char *proc_pid_status)
+static int get_proc_fs_id(const char *proc_pid_status, char type)
 {
-    int real, euid, saved;
-    /* if we fail to parse the uid, then make it root only readable to be safe */
-    int fs_uid = 0;
+    char id_type[] = "_id";
+    id_type[0] = type;
+
+    int real, e_id, saved;
+    int fs_id = 0;
 
     const char *line = proc_pid_status; /* never NULL */
     for (;;)
     {
-        if (strncmp(line, "Uid", 3) == 0)
+        if (strncmp(line, id_type, 3) == 0)
         {
-            int n = sscanf(line, "Uid:\t%d\t%d\t%d\t%d\n", &real, &euid, &saved, &fs_uid);
+            int n = sscanf(line, "%*cid:\t%d\t%d\t%d\t%d\n", &real, &e_id, &saved, &fs_id);
             if (n != 4)
+            {
+                error_msg("Failed to parser /proc/[pid]/status: invalid format of '%cui:' line", type);
                 return -1;
-            break;
+            }
+            return fs_id;
         }
         line = strchr(line, '\n');
         if (!line)
@@ -210,7 +215,18 @@ int get_fsuid(const char *proc_pid_status)
         line++;
     }
 
-    return fs_uid;
+    error_msg("Failed to parser /proc/[pid]/status: not found '%cui:' line", type);
+    return -2;
+}
+
+int get_fsuid(const char *proc_pid_status)
+{
+    return get_proc_fs_id(proc_pid_status, /*UID*/'U');
+}
+
+int get_fsgid(const char *proc_pid_status)
+{
+    return get_proc_fs_id(proc_pid_status, /*GID*/'G');
 }
 
 int dump_fd_info_ext(const char *dest_filename, const char *proc_pid_fd_path, uid_t uid, gid_t gid)
-- 
2.1.0