Skip to content

Commit c88e934

Browse files
committed
MDEV-25759: is_local_ip function can come to incorrect conclusion
The is_local_ip function that used in Galera SST scripts now incorrectly identifies ip-addresses falling under the "127.0.0.0/8" netmask as non-local ip, although they certainly belong to the loopback interface. This commit fixes this flaw.
1 parent f70b11c commit c88e934

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

scripts/wsrep_sst_common.sh

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,9 @@ get_openssl()
875875
readonly OPENSSL_BINARY
876876
}
877877

878+
#
878879
# Generate a string equivalent to 16 random bytes
880+
#
879881
wsrep_gen_secret()
880882
{
881883
get_openssl
@@ -889,16 +891,36 @@ wsrep_gen_secret()
889891
fi
890892
}
891893

894+
#
895+
# Checking if the address passed to us is local.
896+
# If the second parameter is nonzero, then this function
897+
# does not check for matches with local domain names:
898+
#
892899
is_local_ip()
893900
{
894-
[ "$1" = '127.0.0.1' ] && return 0
895-
[ "$1" = '127.0.0.2' ] && return 0
896-
[ "$1" = 'localhost' ] && return 0
897-
[ "$1" = '[::1]' ] && return 0
898-
[ "$1" = "$(hostname -s)" ] && return 0
899-
[ "$1" = "$(hostname -f)" ] && return 0
900-
[ "$1" = "$(hostname -d)" ] && return 0
901-
901+
# Rapid recognition of the most common cases:
902+
[ "$1" = '127.0.0.1' -o \
903+
"$1" = '127.0.0.2' -o \
904+
"$1" = 'localhost' -o \
905+
"$1" = '[::1]' ] && return 0
906+
# If the address starts with "127." this is probably a local
907+
# address, but we need to clarify what follows this prefix:
908+
if [ "${1#127.}" != "$1" ]; then
909+
# All 127.0.0.0/8 addresses are local:
910+
if echo "$1" | grep -q -E '^127\.[0-9]+\.[0-9]+\.[0-9]+$'; then
911+
return 0
912+
fi
913+
fi
914+
# If the second parameter is nonzero, then we will skip
915+
# the domain name check:
916+
if [ "${2:-0}" -eq 0 ]; then
917+
# We consider all the names of a given host to be local addresses:
918+
[ "$1" = "$(hostname -s)" -o \
919+
"$1" = "$(hostname -f)" -o \
920+
"$1" = "$(hostname -d)" ] && return 0
921+
fi
922+
# Now let's check if the given address is assigned to
923+
# one of the network cards:
902924
local ip_util="$(command -v ip)"
903925
if [ -n "$ip_util" ]; then
904926
# ip address show ouput format is " inet[6] <address>/<mask>":
@@ -914,7 +936,6 @@ is_local_ip()
914936
| grep -F " $1 " >/dev/null && return 0
915937
fi
916938
fi
917-
918939
return 1
919940
}
920941

scripts/wsrep_sst_rsync.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,27 @@ else
250250
CAFILE_OPT=""
251251
fi
252252

253+
VERIFY_OPT=""
254+
CHECK_OPT=""
255+
CHECK_OPT_LOCAL=""
253256
if [ "${SSLMODE#VERIFY}" != "$SSLMODE" ]
254257
then
255258
case "$SSLMODE" in
256259
'VERIFY_IDENTITY')
257260
VERIFY_OPT='verifyPeer = yes'
258-
CHECK_OPT=""
259261
;;
260262
'VERIFY_CA')
261263
VERIFY_OPT='verifyChain = yes'
262-
if is_local_ip "$WSREP_SST_OPT_HOST_UNESCAPED"; then
263-
CHECK_OPT='checkHost = localhost'
264+
# check if the address is an ip-address (v4 or v6):
265+
if echo "$WSREP_SST_OPT_HOST_UNESCAPED" | \
266+
grep -q -E '^([0-9]+(\.[0-9]+){3,3}|[0-9a-fA-F]?(\:[0-9a-fA-F]*)+)$'
267+
then
268+
CHECK_OPT="checkIP = $WSREP_SST_OPT_HOST_UNESCAPED"
264269
else
265-
CHECK_OPT='checkHost = $WSREP_SST_OPT_HOST_UNESCAPED'
270+
CHECK_OPT="checkHost = $WSREP_SST_OPT_HOST"
271+
fi
272+
if is_local_ip "$WSREP_SST_OPT_HOST_UNESCAPED"; then
273+
CHECK_OPT_LOCAL="checkHost = localhost"
266274
fi
267275
;;
268276
*)
@@ -273,9 +281,6 @@ then
273281
wsrep_log_error "Can't have ssl-mode='$SSLMODE' without CA file"
274282
exit 22 # EINVAL
275283
fi
276-
else
277-
VERIFY_OPT=""
278-
CHECK_OPT=""
279284
fi
280285

281286
STUNNEL=""
@@ -310,6 +315,7 @@ connect = $WSREP_SST_OPT_HOST_UNESCAPED:$WSREP_SST_OPT_PORT
310315
TIMEOUTclose = 0
311316
${VERIFY_OPT}
312317
${CHECK_OPT}
318+
${CHECK_OPT_LOCAL}
313319
EOF
314320
fi
315321

@@ -566,6 +572,9 @@ foreground = yes
566572
pid = $STUNNEL_PID
567573
debug = warning
568574
client = no
575+
${VERIFY_OPT}
576+
${CHECK_OPT}
577+
${CHECK_OPT_LOCAL}
569578
[rsync]
570579
accept = $STUNNEL_ACCEPT
571580
exec = $(command -v rsync)
@@ -583,6 +592,9 @@ foreground = yes
583592
pid = $STUNNEL_PID
584593
debug = warning
585594
client = no
595+
${VERIFY_OPT}
596+
${CHECK_OPT}
597+
${CHECK_OPT_LOCAL}
586598
[rsync]
587599
accept = $STUNNEL_ACCEPT
588600
exec = $SHELL

0 commit comments

Comments
 (0)