dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

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

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