Blame 0078-lib-parse-fsgid-and-don-t-fall-back-to-0.patch

3a204fb
From b3f2d99ce6ab19b21a115decac0495d659153837 Mon Sep 17 00:00:00 2001
3a204fb
From: Jakub Filak <jfilak@redhat.com>
3a204fb
Date: Thu, 14 May 2015 10:11:36 +0200
3a204fb
Subject: [PATCH] lib: parse fsgid and don't fall back to 0
3a204fb
3a204fb
Let users of API to fall back to 0 on their own and provide them with a
3a204fb
function for getting fsgid from /proc/[pid]/status.
3a204fb
3a204fb
Thanks Florian Weimer <fweimer@redhat.com>
3a204fb
3a204fb
Signed-off-by: Jakub Filak <jfilak@redhat.com>
3a204fb
---
3a204fb
 src/include/internal_libreport.h |  2 ++
3a204fb
 src/lib/get_cmdline.c            | 32 ++++++++++++++++++++++++--------
3a204fb
 2 files changed, 26 insertions(+), 8 deletions(-)
3a204fb
3a204fb
diff --git a/src/include/internal_libreport.h b/src/include/internal_libreport.h
3a204fb
index 66f91e9..25af681 100644
3a204fb
--- a/src/include/internal_libreport.h
3a204fb
+++ b/src/include/internal_libreport.h
3a204fb
@@ -660,6 +660,8 @@ char* get_cwd(pid_t pid);
3a204fb
 char* get_rootdir(pid_t pid);
3a204fb
 #define get_fsuid libreport_get_fsuid
3a204fb
 int get_fsuid(const char *proc_pid_status);
3a204fb
+#define get_fsgid libreport_get_fsgid
3a204fb
+int get_fsgid(const char *proc_pid_status);
3a204fb
 #define dump_fd_info_ext libreport_dump_fd_info_ext
3a204fb
 int dump_fd_info_ext(const char *dest_filename, const char *proc_pid_fd_path, uid_t uid, gid_t gid);
3a204fb
 #define dump_fd_info libreport_dump_fd_info
3a204fb
diff --git a/src/lib/get_cmdline.c b/src/lib/get_cmdline.c
3a204fb
index c55de30..5547877 100644
3a204fb
--- a/src/lib/get_cmdline.c
3a204fb
+++ b/src/lib/get_cmdline.c
3a204fb
@@ -188,21 +188,26 @@ char* get_rootdir(pid_t pid)
3a204fb
     return malloc_readlink(buf);
3a204fb
 }
3a204fb
 
3a204fb
-int get_fsuid(const char *proc_pid_status)
3a204fb
+static int get_proc_fs_id(const char *proc_pid_status, char type)
3a204fb
 {
3a204fb
-    int real, euid, saved;
3a204fb
-    /* if we fail to parse the uid, then make it root only readable to be safe */
3a204fb
-    int fs_uid = 0;
3a204fb
+    char id_type[] = "_id";
3a204fb
+    id_type[0] = type;
3a204fb
+
3a204fb
+    int real, e_id, saved;
3a204fb
+    int fs_id = 0;
3a204fb
 
3a204fb
     const char *line = proc_pid_status; /* never NULL */
3a204fb
     for (;;)
3a204fb
     {
3a204fb
-        if (strncmp(line, "Uid", 3) == 0)
3a204fb
+        if (strncmp(line, id_type, 3) == 0)
3a204fb
         {
3a204fb
-            int n = sscanf(line, "Uid:\t%d\t%d\t%d\t%d\n", &real, &euid, &saved, &fs_uid);
3a204fb
+            int n = sscanf(line, "%*cid:\t%d\t%d\t%d\t%d\n", &real, &e_id, &saved, &fs_id);
3a204fb
             if (n != 4)
3a204fb
+            {
3a204fb
+                error_msg("Failed to parser /proc/[pid]/status: invalid format of '%cui:' line", type);
3a204fb
                 return -1;
3a204fb
-            break;
3a204fb
+            }
3a204fb
+            return fs_id;
3a204fb
         }
3a204fb
         line = strchr(line, '\n');
3a204fb
         if (!line)
3a204fb
@@ -210,7 +215,18 @@ int get_fsuid(const char *proc_pid_status)
3a204fb
         line++;
3a204fb
     }
3a204fb
 
3a204fb
-    return fs_uid;
3a204fb
+    error_msg("Failed to parser /proc/[pid]/status: not found '%cui:' line", type);
3a204fb
+    return -2;
3a204fb
+}
3a204fb
+
3a204fb
+int get_fsuid(const char *proc_pid_status)
3a204fb
+{
3a204fb
+    return get_proc_fs_id(proc_pid_status, /*UID*/'U');
3a204fb
+}
3a204fb
+
3a204fb
+int get_fsgid(const char *proc_pid_status)
3a204fb
+{
3a204fb
+    return get_proc_fs_id(proc_pid_status, /*GID*/'G');
3a204fb
 }
3a204fb
 
3a204fb
 int dump_fd_info_ext(const char *dest_filename, const char *proc_pid_fd_path, uid_t uid, gid_t gid)
3a204fb
-- 
3a204fb
2.1.0
3a204fb