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