Blame SOURCES/unbound-1.13.1-rh1952814.patch

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