Blame SOURCES/unbound-1.13.1-rh1952814.patch

55b5be
diff --git a/config.h.in b/config.h.in
55b5be
index 103ad9f..0bb29d9 100644
55b5be
--- a/config.h.in
55b5be
+++ b/config.h.in
55b5be
@@ -847,6 +847,14 @@
55b5be
 /* Define if you enable libevent */
55b5be
 #undef USE_LIBEVENT
55b5be
 
55b5be
+/* WARNING! This is only for the libunbound on Linux and does not affect
55b5be
+   unbound resolving daemon itself. This may severely limit the number of
55b5be
+   available outgoing ports and thus decrease randomness. Define this only
55b5be
+   when the target system restricts (e.g. some of SELinux enabled
55b5be
+   distributions) the use of non-ephemeral ports. Define this to enable use of
55b5be
+   /proc/sys/net/ipv4/ip_local_port_range as a default outgoing port range. */
55b5be
+#undef USE_LINUX_IP_LOCAL_PORT_RANGE
55b5be
+
55b5be
 /* Define if you want to use internal select based events */
55b5be
 #undef USE_MINI_EVENT
55b5be
 
55b5be
diff --git a/configure b/configure
55b5be
index c91e8a3..826dce9 100755
55b5be
--- a/configure
55b5be
+++ b/configure
55b5be
@@ -898,6 +898,7 @@ enable_ipsecmod
55b5be
 enable_ipset
55b5be
 with_libmnl
55b5be
 enable_explicit_port_randomisation
55b5be
+enable_linux_ip_local_port_range
55b5be
 with_libunbound_only
55b5be
 '
55b5be
       ac_precious_vars='build_alias
55b5be
@@ -1590,6 +1591,16 @@ Optional Features:
55b5be
   --disable-explicit-port-randomisation
55b5be
                           disable explicit source port randomisation and rely
55b5be
                           on the kernel to provide random source ports
55b5be
+  --enable-linux-ip-local-port-range
55b5be
+                          WARNING! This is only for the libunbound on Linux
55b5be
+                          and does not affect unbound resolving daemon itself.
55b5be
+                          This may severely limit the number of available
55b5be
+                          outgoing ports and thus decrease randomness. Use
55b5be
+                          this option only when the target system restricts
55b5be
+                          the use of non-ephemeral ports. (e.g. some of
55b5be
+                          SELinux enabled distributions) Enable this option to
55b5be
+                          use /proc/sys/net/ipv4/ip_local_port_range as a
55b5be
+                          default outgoing port range
55b5be
 
55b5be
 Optional Packages:
55b5be
   --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
55b5be
@@ -4202,6 +4213,13 @@ else
55b5be
 	else on_mingw="no"; fi
55b5be
 fi
55b5be
 
55b5be
+# are we on Linux?
55b5be
+if uname -s 2>&1 | grep -i linux >/dev/null; then on_linux="yes"
55b5be
+else
55b5be
+	if echo $host $target | grep linux >/dev/null; then on_linux="yes"
55b5be
+	else on_linux="no"; fi
55b5be
+fi
55b5be
+
55b5be
 #
55b5be
 # Determine configuration file
55b5be
 # the eval is to evaluate shell expansion twice
55b5be
@@ -21588,6 +21606,23 @@ $as_echo "#define DISABLE_EXPLICIT_PORT_RANDOMISATION 1" >>confdefs.h
55b5be
 		;;
55b5be
 esac
55b5be
 
55b5be
+if test $on_linux = "yes"; then
55b5be
+	# Check whether --enable-linux-ip-local-port-range was given.
55b5be
+if test "${enable_linux_ip_local_port_range+set}" = set; then :
55b5be
+  enableval=$enable_linux_ip_local_port_range;
55b5be
+fi
55b5be
+
55b5be
+	case "$enable_linux_ip_local_port_range" in
55b5be
+		yes)
55b5be
+
55b5be
+$as_echo "#define USE_LINUX_IP_LOCAL_PORT_RANGE 1" >>confdefs.h
55b5be
+
55b5be
+			;;
55b5be
+		no|*)
55b5be
+			;;
55b5be
+	esac
55b5be
+fi
55b5be
+
55b5be
 
55b5be
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking if ${MAKE:-make} supports $< with implicit rule in scope" >&5
55b5be
 $as_echo_n "checking if ${MAKE:-make} supports $< with implicit rule in scope... " >&6; }
55b5be
diff --git a/configure.ac b/configure.ac
55b5be
index 2d88048..1207047 100644
55b5be
--- a/configure.ac
55b5be
+++ b/configure.ac
55b5be
@@ -152,6 +152,13 @@ else
55b5be
 	else on_mingw="no"; fi
55b5be
 fi
55b5be
 
55b5be
+# are we on Linux?
55b5be
+if uname -s 2>&1 | grep -i linux >/dev/null; then on_linux="yes"
55b5be
+else
55b5be
+	if echo $host $target | grep linux >/dev/null; then on_linux="yes"
55b5be
+	else on_linux="no"; fi
55b5be
+fi
55b5be
+
55b5be
 #
55b5be
 # Determine configuration file
55b5be
 # the eval is to evaluate shell expansion twice
55b5be
@@ -1847,6 +1854,17 @@ case "$enable_explicit_port_randomisation" in
55b5be
 		;;
55b5be
 esac
55b5be
 
55b5be
+if test $on_linux = "yes"; then
55b5be
+	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]))
55b5be
+	case "$enable_linux_ip_local_port_range" in
55b5be
+		yes)
55b5be
+			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.])
55b5be
+			;;
55b5be
+		no|*)
55b5be
+			;;
55b5be
+	esac
55b5be
+fi
55b5be
+
55b5be
 
55b5be
 AC_MSG_CHECKING([if ${MAKE:-make} supports $< with implicit rule in scope])
55b5be
 # on openBSD, the implicit rule make $< work.
55b5be
diff --git a/libunbound/context.c b/libunbound/context.c
55b5be
index cff2831..48d76d9 100644
55b5be
--- a/libunbound/context.c
55b5be
+++ b/libunbound/context.c
55b5be
@@ -69,6 +69,7 @@ context_finalize(struct ub_ctx* ctx)
55b5be
 	} else {
55b5be
 		log_init(cfg->logfile, cfg->use_syslog, NULL);
55b5be
 	}
55b5be
+	cfg_apply_local_port_policy(cfg, 65536);
55b5be
 	config_apply(cfg);
55b5be
 	if(!modstack_setup(&ctx->mods, cfg->module_conf, ctx->env))
55b5be
 		return UB_INITFAIL;
55b5be
diff --git a/util/config_file.c b/util/config_file.c
55b5be
index 4d87dee..6b90e48 100644
55b5be
--- a/util/config_file.c
55b5be
+++ b/util/config_file.c
55b5be
@@ -1681,6 +1681,37 @@ int cfg_condense_ports(struct config_file* cfg, int** avail)
55b5be
 	return num;
55b5be
 }
55b5be
 
55b5be
+void cfg_apply_local_port_policy(struct config_file* cfg, int num) {
55b5be
+(void)cfg;
55b5be
+(void)num;
55b5be
+#ifdef USE_LINUX_IP_LOCAL_PORT_RANGE
55b5be
+	{
55b5be
+		int i = 0;
55b5be
+		FILE* range_fd;
55b5be
+		if ((range_fd = fopen(LINUX_IP_LOCAL_PORT_RANGE_PATH, "r")) != NULL) {
55b5be
+			int min_port = 0;
55b5be
+			int max_port = num - 1;
55b5be
+			if (fscanf(range_fd, "%d %d", &min_port, &max_port) == 2) {
55b5be
+				for(i=0; i
55b5be
+					cfg->outgoing_avail_ports[i] = 0;
55b5be
+				}
55b5be
+				for(i=max_port+1; i
55b5be
+					cfg->outgoing_avail_ports[i] = 0;
55b5be
+				}
55b5be
+			} else {
55b5be
+				log_err("unexpected port range in %s",
55b5be
+						LINUX_IP_LOCAL_PORT_RANGE_PATH);
55b5be
+			}
55b5be
+			fclose(range_fd);
55b5be
+		} else {
55b5be
+			log_warn("failed to read from file: %s (%s)",
55b5be
+					LINUX_IP_LOCAL_PORT_RANGE_PATH,
55b5be
+					strerror(errno));
55b5be
+		}
55b5be
+	}
55b5be
+#endif
55b5be
+}
55b5be
+
55b5be
 /** print error with file and line number */
55b5be
 static void ub_c_error_va_list(const char *fmt, va_list args)
55b5be
 {
55b5be
diff --git a/util/config_file.h b/util/config_file.h
55b5be
index 7cf27cc..d091ef7 100644
55b5be
--- a/util/config_file.h
55b5be
+++ b/util/config_file.h
55b5be
@@ -1172,6 +1172,13 @@ int cfg_mark_ports(const char* str, int allow, int* avail, int num);
55b5be
  */
55b5be
 int cfg_condense_ports(struct config_file* cfg, int** avail);
55b5be
 
55b5be
+/**
55b5be
+ * Apply system specific port range policy.
55b5be
+ * @param cfg: config file.
55b5be
+ * @param num: size of the array (65536).
55b5be
+ */
55b5be
+void cfg_apply_local_port_policy(struct config_file* cfg, int num);
55b5be
+
55b5be
 /**
55b5be
  * Scan ports available
55b5be
  * @param avail: the array from cfg.
55b5be
@@ -1301,5 +1308,9 @@ void w_config_adjust_directory(struct config_file* cfg);
55b5be
 /** debug option for unit tests. */
55b5be
 extern int fake_dsa, fake_sha1;
55b5be
 
55b5be
+#ifdef USE_LINUX_IP_LOCAL_PORT_RANGE
55b5be
+#define LINUX_IP_LOCAL_PORT_RANGE_PATH "/proc/sys/net/ipv4/ip_local_port_range"
55b5be
+#endif
55b5be
+
55b5be
 #endif /* UTIL_CONFIG_FILE_H */
55b5be