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

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