Blame SOURCES/0147-include-debug-don-t-print-pointer-address-for-SUID-p.patch

304038
From 3fcd52706b6818e785a104ed6c4f2b46e5d1ab2f Mon Sep 17 00:00:00 2001
304038
From: Karel Zak <kzak@redhat.com>
304038
Date: Fri, 12 Jan 2018 11:01:26 +0100
304038
Subject: [PATCH] include/debug: don't print pointer address for SUID programs
304038
304038
* introduce new flag __UL_DEBUG_FL_NOADDR to suppress pointer address printing
304038
  (and MNT_DEBUG_FL_NOADDR for libmount)
304038
304038
* use __UL_DEBUG_FL_NOADDR when SUID
304038
304038
* move ul_debugobj() to debugobj.h, and require UL_DEBUG_CURRENT_MASK
304038
  to provide access to the current mask from ul_debugobj(). It's better
304038
  than modify all ul_debugobj() calls and use the global mask as
304038
  argument.
304038
304038
* remove never used UL_DEBUG_DEFINE_FLAG
304038
304038
* remove %p from another libmount and libblkid debug messages
304038
304038
Upstream: http://github.com/karelzak/util-linux/commit/6d00cfb2330cb47d00d350eedfbffbbf5991a743
304038
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1534893
304038
Reported-by: halfdog <me@halfdog.net>
304038
Signed-off-by: Karel Zak <kzak@redhat.com>
304038
---
304038
 include/Makemodule.am                |  2 ++
304038
 include/debug.h                      | 29 +++++++++++------------------
304038
 include/debugobj.h                   | 22 ++++++++++++++++++++++
304038
 lib/loopdev.c                        |  2 +-
304038
 libblkid/src/partitions/partitions.c | 20 ++++++++++----------
304038
 libblkid/src/probe.c                 | 21 ++++++++++-----------
304038
 libmount/src/fs.c                    |  2 +-
304038
 libmount/src/init.c                  |  5 +++++
304038
 libmount/src/mountP.h                |  4 +++-
304038
 libmount/src/tab_diff.c              |  6 +++---
304038
 libmount/src/tab_update.c            |  4 ++--
304038
 libsmartcols/src/smartcolsP.h        |  3 +++
304038
 sys-utils/lsns.c                     |  3 +++
304038
 13 files changed, 76 insertions(+), 47 deletions(-)
304038
 create mode 100644 include/debugobj.h
304038
304038
diff --git a/include/Makemodule.am b/include/Makemodule.am
304038
index 168029683..bd4aa8cea 100644
304038
--- a/include/Makemodule.am
304038
+++ b/include/Makemodule.am
304038
@@ -11,6 +11,8 @@ dist_noinst_HEADERS += \
304038
 	include/colors.h \
304038
 	include/cpuset.h \
304038
 	include/crc32.h \
304038
+	include/debug.h \
304038
+	include/debugobj.h \
304038
 	include/env.h \
304038
 	include/exec_shell.h \
304038
 	include/exitcodes.h \
304038
diff --git a/include/debug.h b/include/debug.h
304038
index 848e47456..1c7ed8037 100644
304038
--- a/include/debug.h
304038
+++ b/include/debug.h
304038
@@ -13,12 +13,15 @@
304038
 struct dbg_mask { char *mname; int val; };
304038
 #define UL_DEBUG_EMPTY_MASKNAMES {{ NULL, 0 }}
304038
 
304038
-#define UL_DEBUG_DEFINE_MASK(m) int m ## _debug_mask
304038
+#define UL_DEBUG_MASK(m)         m ## _debug_mask
304038
+#define UL_DEBUG_DEFINE_MASK(m)  int UL_DEBUG_MASK(m)
304038
 #define UL_DEBUG_DECLARE_MASK(m) extern UL_DEBUG_DEFINE_MASK(m)
304038
 #define UL_DEBUG_DEFINE_MASKNAMES(m) static const struct dbg_mask m ## _masknames[]
304038
 
304038
-/* p - flag prefix, m - flag postfix */
304038
-#define UL_DEBUG_DEFINE_FLAG(p, m) p ## m
304038
+/*
304038
+ * Internal mask flags (above 0xffffff)
304038
+ */
304038
+#define __UL_DEBUG_FL_NOADDR   (1 << 24)       /* Don't print object address */
304038
 
304038
 /* l - library name, p - flag prefix, m - flag postfix, x - function */
304038
 #define __UL_DBG(l, p, m, x) \
304038
@@ -55,6 +58,10 @@ struct dbg_mask { char *mname; int val; };
304038
 				lib ## _debug_mask = parse_envmask(lib ## _masknames, str); \
304038
 		} else \
304038
 			lib ## _debug_mask = mask; \
304038
+		if (lib ## _debug_mask) { \
304038
+			if (getuid() != geteuid() || getgid() != getegid()) \
304038
+				lib ## _debug_mask |= __UL_DEBUG_FL_NOADDR; \
304038
+		} \
304038
 		lib ## _debug_mask |= pref ## INIT; \
304038
 		if (lib ## _debug_mask != pref ## INIT) { \
304038
 			__UL_DBG(lib, pref, INIT, ul_debug("library debug mask: 0x%04x", \
304038
@@ -72,21 +79,7 @@ ul_debug(const char *mesg, ...)
304038
 	va_end(ap);
304038
 	fputc('\n', stderr);
304038
 }
304038
-
304038
-static inline void __attribute__ ((__format__ (__printf__, 2, 3)))
304038
-ul_debugobj(void *handler, const char *mesg, ...)
304038
-{
304038
-	va_list ap;
304038
-
304038
-	if (handler)
304038
-		fprintf(stderr, "[%p]: ", handler);
304038
-	va_start(ap, mesg);
304038
-	vfprintf(stderr, mesg, ap);
304038
-	va_end(ap);
304038
-	fputc('\n', stderr);
304038
-}
304038
-
304038
-static inline int parse_envmask(const struct dbg_mask const flagnames[],
304038
+static inline int parse_envmask(const struct dbg_mask flagnames[],
304038
 				const char *mask)
304038
 {
304038
 	int res;
304038
diff --git a/include/debugobj.h b/include/debugobj.h
304038
new file mode 100644
304038
index 000000000..73b70b8df
304038
--- /dev/null
304038
+++ b/include/debugobj.h
304038
@@ -0,0 +1,22 @@
304038
+#ifndef UTIL_LINUX_DEBUGOBJ_H
304038
+#define UTIL_LINUX_DEBUGOBJ_H
304038
+
304038
+/*
304038
+ * Include *after* debug.h and after UL_DEBUG_CURRENT_MASK define.
304038
+ */
304038
+
304038
+static inline void __attribute__ ((__format__ (__printf__, 2, 3)))
304038
+ul_debugobj(const void *handler, const char *mesg, ...)
304038
+{
304038
+	va_list ap;
304038
+
304038
+	if (handler && !(UL_DEBUG_CURRENT_MASK & __UL_DEBUG_FL_NOADDR))
304038
+		fprintf(stderr, "[%p]: ", handler);
304038
+
304038
+	va_start(ap, mesg);
304038
+	vfprintf(stderr, mesg, ap);
304038
+	va_end(ap);
304038
+	fputc('\n', stderr);
304038
+}
304038
+
304038
+#endif /* UTIL_LINUX_DEBUGOBJ_H */
304038
diff --git a/lib/loopdev.c b/lib/loopdev.c
304038
index db5463698..daf0a81e8 100644
304038
--- a/lib/loopdev.c
304038
+++ b/lib/loopdev.c
304038
@@ -50,7 +50,7 @@
304038
 
304038
 # define DBG(l,x)	do { \
304038
 				if ((l)->debug) {\
304038
-					fprintf(stderr, "loopdev:  [%p]: ", (l)); \
304038
+					fprintf(stderr, "loopdev: "); \
304038
 					x; \
304038
 				} \
304038
 			} while(0)
304038
diff --git a/libblkid/src/partitions/partitions.c b/libblkid/src/partitions/partitions.c
304038
index 9d846ff85..2d0d70d81 100644
304038
--- a/libblkid/src/partitions/partitions.c
304038
+++ b/libblkid/src/partitions/partitions.c
304038
@@ -381,8 +381,8 @@ static blkid_partlist partitions_init_data(struct blkid_chain *chn)
304038
 
304038
 	reset_partlist(ls);
304038
 
304038
-	DBG(LOWPROBE, blkid_debug("parts: initialized partitions list (%p, size=%d)",
304038
-		ls, ls->nparts_max));
304038
+	DBG(LOWPROBE, blkid_debug("parts: initialized partitions list (size=%d)",
304038
+		ls->nparts_max));
304038
 	return ls;
304038
 }
304038
 
304038
@@ -417,7 +417,7 @@ blkid_parttable blkid_partlist_new_parttable(blkid_partlist ls,
304038
 	list_add_tail(&tab->t_tabs, &ls->l_tabs);
304038
 
304038
 	DBG(LOWPROBE, blkid_debug("parts: create a new partition table "
304038
-		       "(%p, type=%s, offset=%"PRId64")", tab, type, offset));
304038
+		       "(type=%s, offset=%"PRId64")", type, offset));
304038
 	return tab;
304038
 }
304038
 
304038
@@ -458,9 +458,9 @@ blkid_partition blkid_partlist_add_partition(blkid_partlist ls,
304038
 	par->start = start;
304038
 	par->size = size;
304038
 
304038
-	DBG(LOWPROBE, blkid_debug("parts: add partition (%p start=%"
304038
-		PRId64 ", size=%" PRId64 ", table=%p)",
304038
-		par, par->start, par->size, tab));
304038
+	DBG(LOWPROBE, blkid_debug("parts: add partition (start=%"
304038
+		PRId64 ", size=%" PRId64 ")",
304038
+		par->start, par->size));
304038
 	return par;
304038
 }
304038
 
304038
@@ -662,8 +662,8 @@ int blkid_partitions_do_subprobe(blkid_probe pr, blkid_partition parent,
304038
 	blkid_loff_t sz, off;
304038
 
304038
 	DBG(LOWPROBE, blkid_debug(
304038
-		"parts: ----> %s subprobe requested (parent=%p)",
304038
-		id->name, parent));
304038
+		"parts: ----> %s subprobe requested)",
304038
+		id->name));
304038
 
304038
 	if (!pr || !parent || !parent->size)
304038
 		return -EINVAL;
304038
@@ -709,8 +709,8 @@ int blkid_partitions_do_subprobe(blkid_probe pr, blkid_partition parent,
304038
 	blkid_free_probe(prc);	/* free cloned prober */
304038
 
304038
 	DBG(LOWPROBE, blkid_debug(
304038
-		"parts: <---- %s subprobe done (parent=%p, rc=%d)",
304038
-		id->name, parent, rc));
304038
+		"parts: <---- %s subprobe done (rc=%d)",
304038
+		id->name, rc));
304038
 
304038
 	return rc;
304038
 }
304038
diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
304038
index 9cf099ae4..07b08441f 100644
304038
--- a/libblkid/src/probe.c
304038
+++ b/libblkid/src/probe.c
304038
@@ -145,7 +145,7 @@ blkid_probe blkid_new_probe(void)
304038
 	if (!pr)
304038
 		return NULL;
304038
 
304038
-	DBG(LOWPROBE, blkid_debug("allocate a new probe %p", pr));
304038
+	DBG(LOWPROBE, blkid_debug("allocate a new probe"));
304038
 
304038
 	/* initialize chains */
304038
 	for (i = 0; i < BLKID_NCHAINS; i++) {
304038
@@ -260,7 +260,7 @@ void blkid_free_probe(blkid_probe pr)
304038
 	blkid_probe_reset_buffer(pr);
304038
 	blkid_free_probe(pr->disk_probe);
304038
 
304038
-	DBG(LOWPROBE, blkid_debug("free probe %p", pr));
304038
+	DBG(LOWPROBE, blkid_debug("free probe"));
304038
 	free(pr);
304038
 }
304038
 
304038
@@ -552,8 +552,8 @@ unsigned char *blkid_probe_get_buffer(blkid_probe pr,
304038
 				list_entry(p, struct blkid_bufinfo, bufs);
304038
 
304038
 		if (x->off <= off && off + len <= x->off + x->len) {
304038
-			DBG(LOWPROBE, blkid_debug("\treuse buffer: off=%jd len=%jd pr=%p",
304038
-							x->off, x->len, pr));
304038
+			DBG(LOWPROBE, blkid_debug("\treuse buffer: off=%jd len=%jd",
304038
+							x->off, x->len));
304038
 			bf = x;
304038
 			break;
304038
 		}
304038
@@ -584,8 +584,8 @@ unsigned char *blkid_probe_get_buffer(blkid_probe pr,
304038
 		bf->off = off;
304038
 		INIT_LIST_HEAD(&bf->bufs);
304038
 
304038
-		DBG(LOWPROBE, blkid_debug("\tbuffer read: off=%jd len=%jd pr=%p",
304038
-				off, len, pr));
304038
+		DBG(LOWPROBE, blkid_debug("\tbuffer read: off=%jd len=%jd",
304038
+				off, len));
304038
 
304038
 		ret = read(pr->fd, bf->data, len);
304038
 		if (ret != (ssize_t) len) {
304038
@@ -609,7 +609,7 @@ static void blkid_probe_reset_buffer(blkid_probe pr)
304038
 	if (!pr || list_empty(&pr->buffers))
304038
 		return;
304038
 
304038
-	DBG(LOWPROBE, blkid_debug("reseting probing buffers pr=%p", pr));
304038
+	DBG(LOWPROBE, blkid_debug("reseting probing buffers"));
304038
 
304038
 	while (!list_empty(&pr->buffers)) {
304038
 		struct blkid_bufinfo *bf = list_entry(pr->buffers.next,
304038
@@ -766,9 +766,8 @@ int blkid_probe_set_dimension(blkid_probe pr,
304038
 		return -1;
304038
 
304038
 	DBG(LOWPROBE, blkid_debug(
304038
-		"changing probing area pr=%p: size=%llu, off=%llu "
304038
+		"changing probing area: size=%llu, off=%llu "
304038
 		"-to-> size=%llu, off=%llu",
304038
-		pr,
304038
 		(unsigned long long) pr->size,
304038
 		(unsigned long long) pr->off,
304038
 		(unsigned long long) size,
304038
@@ -840,7 +839,7 @@ int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id,
304038
 static inline void blkid_probe_start(blkid_probe pr)
304038
 {
304038
 	if (pr) {
304038
-		DBG(LOWPROBE, blkid_debug("%p: start probe", pr));
304038
+		DBG(LOWPROBE, blkid_debug("start probe"));
304038
 		pr->cur_chain = NULL;
304038
 		pr->prob_flags = 0;
304038
 		blkid_probe_set_wiper(pr, 0, 0);
304038
@@ -850,7 +849,7 @@ static inline void blkid_probe_start(blkid_probe pr)
304038
 static inline void blkid_probe_end(blkid_probe pr)
304038
 {
304038
 	if (pr) {
304038
-		DBG(LOWPROBE, blkid_debug("%p: end probe", pr));
304038
+		DBG(LOWPROBE, blkid_debug("end probe"));
304038
 		pr->cur_chain = NULL;
304038
 		pr->prob_flags = 0;
304038
 		blkid_probe_set_wiper(pr, 0, 0);
304038
diff --git a/libmount/src/fs.c b/libmount/src/fs.c
304038
index 75e3bbb26..e46ee0c0e 100644
304038
--- a/libmount/src/fs.c
304038
+++ b/libmount/src/fs.c
304038
@@ -1451,7 +1451,7 @@ int mnt_fs_print_debug(struct libmnt_fs *fs, FILE *file)
304038
 {
304038
 	if (!fs || !file)
304038
 		return -EINVAL;
304038
-	fprintf(file, "------ fs: %p\n", fs);
304038
+	fprintf(file, "------ fs\n");
304038
 	fprintf(file, "source: %s\n", mnt_fs_get_source(fs));
304038
 	fprintf(file, "target: %s\n", mnt_fs_get_target(fs));
304038
 	fprintf(file, "fstype: %s\n", mnt_fs_get_fstype(fs));
304038
diff --git a/libmount/src/init.c b/libmount/src/init.c
304038
index 4e5f489c4..e5e6925f5 100644
304038
--- a/libmount/src/init.c
304038
+++ b/libmount/src/init.c
304038
@@ -38,6 +38,11 @@ void mnt_init_debug(int mask)
304038
 	} else
304038
 		libmount_debug_mask = mask;
304038
 
304038
+	if (libmount_debug_mask) {
304038
+		if (getuid() != geteuid() || getgid() != getegid())
304038
+			libmount_debug_mask |= MNT_DEBUG_FL_NOADDR;
304038
+	}
304038
+
304038
 	libmount_debug_mask |= MNT_DEBUG_INIT;
304038
 
304038
 	if (libmount_debug_mask && libmount_debug_mask != MNT_DEBUG_INIT) {
304038
diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h
304038
index 8b3f92e17..dc3ed3f49 100644
304038
--- a/libmount/src/mountP.h
304038
+++ b/libmount/src/mountP.h
304038
@@ -51,6 +51,8 @@
304038
 #define MNT_DEBUG_DIFF		(1 << 11)
304038
 #define MNT_DEBUG_ALL		0xFFFF
304038
 
304038
+#define MNT_DEBUG_FL_NOADDR	(1 << 24)
304038
+
304038
 #ifdef CONFIG_LIBMOUNT_DEBUG
304038
 # include <stdio.h>
304038
 # include <stdarg.h>
304038
@@ -91,7 +93,7 @@ mnt_debug_h(void *handler, const char *mesg, ...)
304038
 {
304038
 	va_list ap;
304038
 
304038
-	if (handler)
304038
+	if (handler && !(libmount_debug_mask & MNT_DEBUG_FL_NOADDR))
304038
 		fprintf(stderr, "[%p]: ", handler);
304038
 	va_start(ap, mesg);
304038
 	vfprintf(stderr, mesg, ap);
304038
diff --git a/libmount/src/tab_diff.c b/libmount/src/tab_diff.c
304038
index f01f889f8..0a69f402c 100644
304038
--- a/libmount/src/tab_diff.c
304038
+++ b/libmount/src/tab_diff.c
304038
@@ -229,9 +229,9 @@ int mnt_diff_tables(struct libmnt_tabdiff *df, struct libmnt_table *old_tab,
304038
 	if (!no && !nn)			/* both tables are empty */
304038
 		return 0;
304038
 
304038
-	DBG(DIFF, mnt_debug_h(df, "analyze new=%p (%d entries), "
304038
-				          "old=%p (%d entries)",
304038
-				new_tab, nn, old_tab, no));
304038
+	DBG(DIFF, mnt_debug_h(df, "analyze new (%d entries), "
304038
+				          "old (%d entries)",
304038
+				nn, no));
304038
 
304038
 	mnt_reset_iter(&itr, MNT_ITER_FORWARD);
304038
 
304038
diff --git a/libmount/src/tab_update.c b/libmount/src/tab_update.c
304038
index 5f503cad7..b45c4a92c 100644
304038
--- a/libmount/src/tab_update.c
304038
+++ b/libmount/src/tab_update.c
304038
@@ -173,8 +173,8 @@ int mnt_update_set_fs(struct libmnt_update *upd, unsigned long mountflags,
304038
 		return -EINVAL;
304038
 
304038
 	DBG(UPDATE, mnt_debug_h(upd,
304038
-			"resetting FS [fs=0x%p, target=%s, flags=0x%08lx]",
304038
-			fs, target, mountflags));
304038
+			"resetting FS [target=%s, flags=0x%08lx]",
304038
+			target, mountflags));
304038
 	if (fs) {
304038
 		DBG(UPDATE, mnt_debug_h(upd, "FS template:"));
304038
 		DBG(UPDATE, mnt_fs_print_debug(fs, stderr));
304038
diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h
304038
index 28246c14f..cea4f3101 100644
304038
--- a/libsmartcols/src/smartcolsP.h
304038
+++ b/libsmartcols/src/smartcolsP.h
304038
@@ -43,6 +43,9 @@ UL_DEBUG_DECLARE_MASK(libsmartcols);
304038
 #define ON_DBG(m, x)	__UL_DBG_CALL(libsmartcols, SCOLS_DEBUG_, m, x)
304038
 #define DBG_FLUSH	__UL_DBG_FLUSH(libsmartcols, SCOLS_DEBUG_)
304038
 
304038
+#define UL_DEBUG_CURRENT_MASK  UL_DEBUG_MASK(libsmartcols)
304038
+#include "debugobj.h"
304038
+
304038
 /*
304038
  * Generic iterator
304038
  */
304038
diff --git a/sys-utils/lsns.c b/sys-utils/lsns.c
304038
index 5ee298172..fb53a16a4 100644
304038
--- a/sys-utils/lsns.c
304038
+++ b/sys-utils/lsns.c
304038
@@ -55,6 +55,9 @@ UL_DEBUG_DEFINE_MASKNAMES(lsns) = UL_DEBUG_EMPTY_MASKNAMES;
304038
 #define DBG(m, x)       __UL_DBG(lsns, LSNS_DEBUG_, m, x)
304038
 #define ON_DBG(m, x)    __UL_DBG_CALL(lsns, LSNS_DEBUG_, m, x)
304038
 
304038
+#define UL_DEBUG_CURRENT_MASK  UL_DEBUG_MASK(lsns)
304038
+#include "debugobj.h"
304038
+
304038
 struct idcache *uid_cache = NULL;
304038
 
304038
 /* column IDs */
304038
-- 
304038
2.13.6
304038