Blame SOURCES/0003-Drop-root-priviledges-before-opening-first-savefile-.patch

f6b043
From 9bee0dffaebbc53b9762df7a6d84a553969e7b00 Mon Sep 17 00:00:00 2001
f6b043
From: rpm-build <rpm-build>
f6b043
Date: Fri, 3 Feb 2017 09:36:26 +0100
f6b043
Subject: [PATCH 3/8] Drop root priviledges before opening first savefile if
f6b043
 running with -Z root
f6b043
f6b043
---
f6b043
 tcpdump.1.in |  7 ++++++-
f6b043
 tcpdump.c    | 30 ++++++++++++++++++++++++++++++
f6b043
 2 files changed, 36 insertions(+), 1 deletion(-)
f6b043
f6b043
diff --git a/tcpdump.1.in b/tcpdump.1.in
f6b043
index f04a579..ca5cff2 100644
f6b043
--- a/tcpdump.1.in
f6b043
+++ b/tcpdump.1.in
f6b043
@@ -249,6 +249,9 @@ have the name specified with the
f6b043
 flag, with a number after it, starting at 1 and continuing upward.
f6b043
 The units of \fIfile_size\fP are millions of bytes (1,000,000 bytes,
f6b043
 not 1,048,576 bytes).
f6b043
+
f6b043
+Note that when used with \fB\-Z\fR option (enabled by default), privileges
f6b043
+are dropped before opening first savefile.
f6b043
 .TP
f6b043
 .B \-d
f6b043
 Dump the compiled packet-matching code in a human readable form to
f6b043
@@ -860,7 +863,9 @@ but before opening any savefiles for output, change the user ID to
f6b043
 and the group ID to the primary group of
f6b043
 .IR user .
f6b043
 .IP
f6b043
-This behavior can also be enabled by default at compile time.
f6b043
+This behavior is enabled by default (\fB\-Z tcpdump\fR), and can
f6b043
+be disabled by \fB\-Z root\fR.
f6b043
+
f6b043
 .IP "\fI expression\fP"
f6b043
 .RS
f6b043
 selects which packets will be dumped.
f6b043
diff --git a/tcpdump.c b/tcpdump.c
f6b043
index 73bf138..29f7f87 100644
f6b043
--- a/tcpdump.c
f6b043
+++ b/tcpdump.c
f6b043
@@ -1133,6 +1133,7 @@ main(int argc, char **argv)
f6b043
 	cap_rights_t rights;
f6b043
 	int cansandbox;
f6b043
 #endif	/* HAVE_CAPSICUM */
f6b043
+	int chown_flag = 0;
f6b043
 	int Oflag = 1;			/* run filter code optimizer */
f6b043
 	int yflag_dlt = -1;
f6b043
 	const char *yflag_dlt_name = NULL;
f6b043
@@ -1843,6 +1844,19 @@ main(int argc, char **argv)
f6b043
 		}
f6b043
 		capng_apply(CAPNG_SELECT_BOTH);
f6b043
 #endif /* HAVE_LIBCAP_NG */
f6b043
+	/* If user is running tcpdump as root and wants to write to the savefile,
f6b043
+	 * we will check if -C is set and if it is, we will drop root
f6b043
+	 * privileges right away and consequent call to>pcap_dump_open()
f6b043
+	 * will most likely fail for the first file. If -C flag is not set we
f6b043
+	 * will create file as root then change ownership of file to proper
f6b043
+	 * user(default tcpdump) and drop root privileges.
f6b043
+	 */
f6b043
+	if (WFileName)
f6b043
+		if (Cflag && (username || chroot_dir))
f6b043
+			droproot(username, chroot_dir);
f6b043
+		else
f6b043
+			chown_flag = 1;
f6b043
+	else
f6b043
 		if (username || chroot_dir)
f6b043
 			droproot(username, chroot_dir);
f6b043
 
f6b043
@@ -1881,6 +1895,22 @@ main(int argc, char **argv)
f6b043
 		  MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
f6b043
 
f6b043
 		p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
f6b043
+
f6b043
+	/* Change ownership of file and drop root privileges */
f6b043
+	if (chown_flag) {
f6b043
+		struct passwd *pwd;
f6b043
+
f6b043
+		pwd = getpwnam(username);
f6b043
+		if (!pwd)
f6b043
+			error("Couldn't find user '%s'", username);
f6b043
+
f6b043
+		if (strcmp(WFileName, "-") && chown(dumpinfo.CurrentFileName, pwd->pw_uid, pwd->pw_gid) < 0)
f6b043
+			error("Couldn't change ownership of savefile");
f6b043
+
f6b043
+		if (username || chroot_dir)
f6b043
+			droproot(username, chroot_dir);
f6b043
+    }
f6b043
+
f6b043
 #ifdef HAVE_LIBCAP_NG
f6b043
 		/* Give up CAP_DAC_OVERRIDE capability.
f6b043
 		 * Only allow it to be restored if the -C or -G flag have been
f6b043
-- 
f6b043
2.9.3
f6b043