3e0169
From 3ec32f9975f262073f8fbdecd2bfaee4a1d3db48 Mon Sep 17 00:00:00 2001
3e0169
From: Iker Pedrosa <ipedrosa@redhat.com>
3e0169
Date: Wed, 13 Jul 2022 09:55:14 +0200
3e0169
Subject: [PATCH] subordinateio: also compare the owner ID
3e0169
3e0169
IDs already populate /etc/subuid and /etc/subgid files so it's necessary
3e0169
not only to check for the owner name but also for the owner ID of a
3e0169
given range.
3e0169
3e0169
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2093311
3e0169
3e0169
Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
3e0169
---
3e0169
 lib/subordinateio.c | 50 +++++++++++++++++++++++++++++++++++++++++++++
3e0169
 1 file changed, 50 insertions(+)
3e0169
3e0169
diff --git a/lib/subordinateio.c b/lib/subordinateio.c
3e0169
index 9ca70b8b..6bc45283 100644
3e0169
--- a/lib/subordinateio.c
3e0169
+++ b/lib/subordinateio.c
3e0169
@@ -17,6 +17,8 @@
3e0169
 #include <ctype.h>
3e0169
 #include <fcntl.h>
3e0169
 
3e0169
+#define ID_SIZE 31
3e0169
+
3e0169
 /*
3e0169
  * subordinate_dup: create a duplicate range
3e0169
  *
3e0169
@@ -745,6 +747,40 @@ gid_t sub_gid_find_free_range(gid_t min, gid_t max, unsigned long count)
3e0169
 	return start == ULONG_MAX ? (gid_t) -1 : start;
3e0169
 }
3e0169
 
3e0169
+static bool get_owner_id(const char *owner, enum subid_type id_type, char *id)
3e0169
+{
3e0169
+	struct passwd *pw;
3e0169
+	struct group *gr;
3e0169
+	int ret = 0;
3e0169
+
3e0169
+	switch (id_type) {
3e0169
+	case ID_TYPE_UID:
3e0169
+		pw = getpwnam(owner);
3e0169
+		if (pw == NULL) {
3e0169
+			return false;
3e0169
+		}
3e0169
+		ret = snprintf(id, ID_SIZE, "%u", pw->pw_uid);
3e0169
+		if (ret < 0 || ret >= ID_SIZE) {
3e0169
+			return false;
3e0169
+		}
3e0169
+		break;
3e0169
+	case ID_TYPE_GID:
3e0169
+		gr = getgrnam(owner);
3e0169
+		if (gr == NULL) {
3e0169
+			return false;
3e0169
+		}
3e0169
+		ret = snprintf(id, ID_SIZE, "%u", gr->gr_gid);
3e0169
+		if (ret < 0 || ret >= ID_SIZE) {
3e0169
+			return false;
3e0169
+		}
3e0169
+		break;
3e0169
+	default:
3e0169
+		return false;
3e0169
+	}
3e0169
+
3e0169
+	return true;
3e0169
+}
3e0169
+
3e0169
 /*
3e0169
  * int list_owner_ranges(const char *owner, enum subid_type id_type, struct subordinate_range ***ranges)
3e0169
  *
3e0169
@@ -770,6 +806,8 @@ int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_r
3e0169
 	enum subid_status status;
3e0169
 	int count = 0;
3e0169
 	struct subid_nss_ops *h;
3e0169
+	char id[ID_SIZE];
3e0169
+	bool have_owner_id;
3e0169
 
3e0169
 	*in_ranges = NULL;
3e0169
 
3e0169
@@ -798,6 +836,8 @@ int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_r
3e0169
 		return -1;
3e0169
 	}
3e0169
 
3e0169
+	have_owner_id = get_owner_id(owner, id_type, id);
3e0169
+
3e0169
 	commonio_rewind(db);
3e0169
 	while ((range = commonio_next(db)) != NULL) {
3e0169
 		if (0 == strcmp(range->owner, owner)) {
3e0169
@@ -808,6 +848,16 @@ int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_r
3e0169
 				goto out;
3e0169
 			}
3e0169
 		}
3e0169
+
3e0169
+		// Let's also compare with the ID
3e0169
+		if (have_owner_id == true && 0 == strcmp(range->owner, id)) {
3e0169
+			if (!append_range(&ranges, range, count++)) {
3e0169
+				free(ranges);
3e0169
+				ranges = NULL;
3e0169
+				count = -1;
3e0169
+				goto out;
3e0169
+			}
3e0169
+		}
3e0169
 	}
3e0169
 
3e0169
 out:
3e0169
-- 
3e0169
2.36.1
3e0169