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

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