Blame SOURCES/unbound-1.7.3-rh1830625.patch

5abe5b
diff --git a/config.h.in b/config.h.in
5abe5b
index 3b06bfa..2beb538 100644
5abe5b
--- a/config.h.in
5abe5b
+++ b/config.h.in
5abe5b
@@ -735,6 +735,14 @@
5abe5b
 /* Define to 1 to use ipsecmod support. */
5abe5b
 #undef USE_IPSECMOD
5abe5b
 
5abe5b
+/* WARNING! This is only for the libunbound on Linux and does not affect
5abe5b
+   unbound resolving daemon itself. This may severely limit the number of
5abe5b
+   available outgoing ports and thus decrease randomness. Define this only
5abe5b
+   when the target system restricts (e.g. some of SELinux enabled
5abe5b
+   distributions) the use of non-ephemeral ports. Define this to enable use of
5abe5b
+   /proc/sys/net/ipv4/ip_local_port_range as a default outgoing port range. */
5abe5b
+#undef USE_LINUX_IP_LOCAL_PORT_RANGE
5abe5b
+
5abe5b
 /* Define if you want to use internal select based events */
5abe5b
 #undef USE_MINI_EVENT
5abe5b
 
5abe5b
diff --git a/configure b/configure
5abe5b
index 16d50d0..36cf3fa 100755
5abe5b
--- a/configure
5abe5b
+++ b/configure
5abe5b
@@ -878,6 +878,7 @@ enable_dnscrypt
5abe5b
 with_libsodium
5abe5b
 enable_cachedb
5abe5b
 enable_ipsecmod
5abe5b
+enable_linux_ip_local_port_range
5abe5b
 with_libunbound_only
5abe5b
 '
5abe5b
       ac_precious_vars='build_alias
5abe5b
@@ -1565,6 +1566,16 @@ Optional Features:
5abe5b
                           storage
5abe5b
   --enable-ipsecmod       Enable ipsecmod module that facilitates
5abe5b
                           opportunistic IPsec
5abe5b
+  --enable-linux-ip-local-port-range
5abe5b
+                          WARNING! This is only for the libunbound on Linux
5abe5b
+                          and does not affect unbound resolving daemon itself.
5abe5b
+                          This may severely limit the number of available
5abe5b
+                          outgoing ports and thus decrease randomness. Use
5abe5b
+                          this option only when the target system restricts
5abe5b
+                          the use of non-ephemeral ports. (e.g. some of
5abe5b
+                          SELinux enabled distributions) Enable this option to
5abe5b
+                          use /proc/sys/net/ipv4/ip_local_port_range as a
5abe5b
+                          default outgoing port range
5abe5b
 
5abe5b
 Optional Packages:
5abe5b
   --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
5abe5b
@@ -4147,6 +4158,13 @@ else
5abe5b
 	else on_mingw="no"; fi
5abe5b
 fi
5abe5b
 
5abe5b
+# are we on Linux?
5abe5b
+if uname -s 2>&1 | grep -i linux >/dev/null; then on_linux="yes"
5abe5b
+else
5abe5b
+	if echo $host $target | grep linux >/dev/null; then on_linux="yes"
5abe5b
+	else on_linux="no"; fi
5abe5b
+fi
5abe5b
+
5abe5b
 #
5abe5b
 # Determine configuration file
5abe5b
 # the eval is to evaluate shell expansion twice
5abe5b
@@ -20899,6 +20917,23 @@ $as_echo "#define USE_IPSECMOD 1" >>confdefs.h
5abe5b
 		;;
5abe5b
 esac
5abe5b
 
5abe5b
+if test $on_linux = "yes"; then
5abe5b
+	# Check whether --enable-linux-ip-local-port-range was given.
5abe5b
+if test "${enable_linux_ip_local_port_range+set}" = set; then :
5abe5b
+  enableval=$enable_linux_ip_local_port_range;
5abe5b
+fi
5abe5b
+
5abe5b
+	case "$enable_linux_ip_local_port_range" in
5abe5b
+		yes)
5abe5b
+
5abe5b
+$as_echo "#define USE_LINUX_IP_LOCAL_PORT_RANGE 1" >>confdefs.h
5abe5b
+
5abe5b
+			;;
5abe5b
+		no|*)
5abe5b
+			;;
5abe5b
+	esac
5abe5b
+fi
5abe5b
+
5abe5b
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking if ${MAKE:-make} supports $< with implicit rule in scope" >&5
5abe5b
 $as_echo_n "checking if ${MAKE:-make} supports $< with implicit rule in scope... " >&6; }
5abe5b
 # on openBSD, the implicit rule make $< work.
5abe5b
diff --git a/configure.ac b/configure.ac
5abe5b
index 1bff4ed..525ad62 100644
5abe5b
--- a/configure.ac
5abe5b
+++ b/configure.ac
5abe5b
@@ -131,6 +131,13 @@ else
5abe5b
 	else on_mingw="no"; fi
5abe5b
 fi
5abe5b
 
5abe5b
+# are we on Linux?
5abe5b
+if uname -s 2>&1 | grep -i linux >/dev/null; then on_linux="yes"
5abe5b
+else
5abe5b
+	if echo $host $target | grep linux >/dev/null; then on_linux="yes"
5abe5b
+	else on_linux="no"; fi
5abe5b
+fi
5abe5b
+
5abe5b
 #
5abe5b
 # Determine configuration file
5abe5b
 # the eval is to evaluate shell expansion twice
5abe5b
@@ -1520,6 +1527,17 @@ case "$enable_ipsecmod" in
5abe5b
 		;;
5abe5b
 esac
5abe5b
 
5abe5b
+if test $on_linux = "yes"; then
5abe5b
+	AC_ARG_ENABLE(linux-ip-local-port-range, AC_HELP_STRING([--enable-linux-ip-local-port-range], [WARNING! This is only for the libunbound on Linux and does not affect unbound resolving daemon itself. This may severely limit the number of available outgoing ports and thus decrease randomness. Use this option only when the target system restricts the use of non-ephemeral ports. (e.g. some of SELinux enabled distributions) Enable this option to use /proc/sys/net/ipv4/ip_local_port_range as a default outgoing port range]))
5abe5b
+	case "$enable_linux_ip_local_port_range" in
5abe5b
+		yes)
5abe5b
+			AC_DEFINE([USE_LINUX_IP_LOCAL_PORT_RANGE], [1], [WARNING! This is only for the libunbound on Linux and does not affect unbound resolving daemon itself. This may severely limit the number of available outgoing ports and thus decrease randomness. Define this only when the target system restricts (e.g. some of SELinux enabled distributions) the use of non-ephemeral ports. Define this to enable use of /proc/sys/net/ipv4/ip_local_port_range as a default outgoing port range.])
5abe5b
+			;;
5abe5b
+		no|*)
5abe5b
+			;;
5abe5b
+	esac
5abe5b
+fi
5abe5b
+
5abe5b
 AC_MSG_CHECKING([if ${MAKE:-make} supports $< with implicit rule in scope])
5abe5b
 # on openBSD, the implicit rule make $< work.
5abe5b
 # on Solaris, it does not work ($? is changed sources, $^ lists dependencies).
5abe5b
diff --git a/libunbound/context.c b/libunbound/context.c
5abe5b
index 6ac8086..e1fbc64 100644
5abe5b
--- a/libunbound/context.c
5abe5b
+++ b/libunbound/context.c
5abe5b
@@ -60,6 +60,7 @@ context_finalize(struct ub_ctx* ctx)
5abe5b
 	if(ctx->logfile_override)
5abe5b
 		log_file(ctx->log_out);
5abe5b
 	else	log_init(cfg->logfile, cfg->use_syslog, NULL);
5abe5b
+	cfg_apply_local_port_policy(cfg, 65536);
5abe5b
 	config_apply(cfg);
5abe5b
 	if(!modstack_setup(&ctx->mods, cfg->module_conf, ctx->env))
5abe5b
 		return UB_INITFAIL;
5abe5b
diff --git a/util/config_file.c b/util/config_file.c
5abe5b
index 68a0a15..eaf1ca3 100644
5abe5b
--- a/util/config_file.c
5abe5b
+++ b/util/config_file.c
5abe5b
@@ -1474,6 +1474,37 @@ int cfg_condense_ports(struct config_file* cfg, int** avail)
5abe5b
 	return num;
5abe5b
 }
5abe5b
 
5abe5b
+void cfg_apply_local_port_policy(struct config_file* cfg, int num) {
5abe5b
+(void)cfg;
5abe5b
+(void)num;
5abe5b
+#ifdef USE_LINUX_IP_LOCAL_PORT_RANGE
5abe5b
+	{
5abe5b
+		int i = 0;
5abe5b
+		FILE* range_fd;
5abe5b
+		if ((range_fd = fopen(LINUX_IP_LOCAL_PORT_RANGE_PATH, "r")) != NULL) {
5abe5b
+			int min_port = 0;
5abe5b
+			int max_port = num - 1;
5abe5b
+			if (fscanf(range_fd, "%d %d", &min_port, &max_port) == 2) {
5abe5b
+				for(i=0; i
5abe5b
+					cfg->outgoing_avail_ports[i] = 0;
5abe5b
+				}
5abe5b
+				for(i=max_port+1; i
5abe5b
+					cfg->outgoing_avail_ports[i] = 0;
5abe5b
+				}
5abe5b
+			} else {
5abe5b
+				log_err("unexpected port range in %s",
5abe5b
+						LINUX_IP_LOCAL_PORT_RANGE_PATH);
5abe5b
+			}
5abe5b
+			fclose(range_fd);
5abe5b
+		} else {
5abe5b
+			log_warn("failed to read from file: %s (%s)",
5abe5b
+					LINUX_IP_LOCAL_PORT_RANGE_PATH,
5abe5b
+					strerror(errno));
5abe5b
+		}
5abe5b
+	}
5abe5b
+#endif
5abe5b
+}
5abe5b
+
5abe5b
 /** print error with file and line number */
5abe5b
 static void ub_c_error_va_list(const char *fmt, va_list args)
5abe5b
 {
5abe5b
diff --git a/util/config_file.h b/util/config_file.h
5abe5b
index 1e7f402..3aa71b7 100644
5abe5b
--- a/util/config_file.h
5abe5b
+++ b/util/config_file.h
5abe5b
@@ -1009,6 +1009,13 @@ int cfg_mark_ports(const char* str, int allow, int* avail, int num);
5abe5b
  */
5abe5b
 int cfg_condense_ports(struct config_file* cfg, int** avail);
5abe5b
 
5abe5b
+/**
5abe5b
+ * Apply system specific port range policy.
5abe5b
+ * @param cfg: config file.
5abe5b
+ * @param num: size of the array (65536).
5abe5b
+ */
5abe5b
+void cfg_apply_local_port_policy(struct config_file* cfg, int num);
5abe5b
+
5abe5b
 /**
5abe5b
  * Scan ports available
5abe5b
  * @param avail: the array from cfg.
5abe5b
@@ -1138,4 +1145,8 @@ void w_config_adjust_directory(struct config_file* cfg);
5abe5b
 /** debug option for unit tests. */
5abe5b
 extern int fake_dsa, fake_sha1;
5abe5b
 
5abe5b
+#ifdef USE_LINUX_IP_LOCAL_PORT_RANGE
5abe5b
+#define LINUX_IP_LOCAL_PORT_RANGE_PATH "/proc/sys/net/ipv4/ip_local_port_range"
5abe5b
+#endif
5abe5b
+
5abe5b
 #endif /* UTIL_CONFIG_FILE_H */