Skip to content

Commit 6a466de

Browse files
committed
feature/network: Increase macaddr validation
Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
1 parent 3a1eac6 commit 6a466de

File tree

5 files changed

+69
-21
lines changed

5 files changed

+69
-21
lines changed

features/network/data/bin/network-cmdline-sh-functions

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ __net_cmdline_sh_functions=1
55

66
. shell-error
77

8+
__valid_hex()
9+
{
10+
while [ "$#" -gt 0 ]; do
11+
[ "${#1}" -eq 2 ] && [ -z "${1#[0-9a-fA-F][0-9a-fA-F]}" ] ||
12+
return 1
13+
shift
14+
done
15+
}
16+
17+
__cmdline_parse_macaddr()
18+
{
19+
case "$#" in
20+
6|20)
21+
__valid_hex "$@" ||
22+
fatal "syntax error: value does not look like a MAC address"
23+
;;
24+
*)
25+
fatal "syntax error: MAC address must contain 6 or 20 components, but got $#"
26+
;;
27+
esac
28+
case "$#" in
29+
6) macaddr="$1:$2:$3:$4:$5:$6" ;;
30+
20) macaddr="$1:$2:$3:$4:$5:$6:$7:$8:$9:${10}:${11}:${12}:${13}:${14}:${15}:${16}:${17}:${18}:${19}:${20}" ;;
31+
esac
32+
}
33+
834
ip_to_var()
935
{
1036
local i v="$1:"
@@ -47,10 +73,8 @@ ip_to_var()
4773
shift 2
4874
[ $# -eq 0 ] ||
4975
{ mtu="$1"; shift; }
50-
[ $# -ne 6 ] ||
51-
{ macaddr="$1:$2:$3:$4:$5:$6"; shift 6; }
5276
[ $# -eq 0 ] ||
53-
fatal "syntax error: macaddr must contain 6 components"
77+
__cmdline_parse_macaddr "$@"
5478
[ -n "$interface" ] ||
5579
fatal "interface name must be non-empty"
5680
return 0
@@ -70,10 +94,8 @@ ip_to_var()
7094

7195
if [ -z "$1" ] || [ -n "${1##*[!0-9]*}" ]; then
7296
mtu="$1"; shift
73-
[ $# -ne 6 ] ||
74-
{ macaddr="$1:$2:$3:$4:$5:$6"; shift 6; }
7597
[ $# -eq 0 ] ||
76-
fatal "syntax error: macaddr must contain 6 components"
98+
__cmdline_parse_macaddr "$@"
7799
return 0
78100
fi
79101

@@ -125,19 +147,11 @@ ifname_to_var()
125147

126148
interface='' macaddr=''
127149

128-
case "$#" in
129-
7)
130-
interface="${1#:}"
131-
macaddr="$2:$3:$4:$5:$6:$7"
132-
;;
133-
21)
134-
interface="${1#:}"
135-
macaddr="$2:$3:$4:$5:$6:$7:$8:$9:${10}:${11}:${12}:${13}:${14}:${15}:${16}:${17}:${18}:${19}:${20}:${21}"
136-
;;
137-
*)
138-
fatal "syntax error: ifname must contain 7 or 21 components"
139-
;;
140-
esac
150+
[ "$#" -gt 1 ] ||
151+
fatal "syntax error: ifname must contain 7 or 21 components"
152+
153+
interface="${1#:}"; shift
154+
__cmdline_parse_macaddr "$@"
141155
}
142156

143157
fi #__net_cmdline_sh_functions

features/network/tests/ts0001-cmdline-ifname/expect

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@ IFNAME: bootif0:00:00:10:29:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:99:4c:42
66
- interface: bootif0
77
- macaddr : 00:00:10:29:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:99:4c:42
88

9+
IFNAME: bootif0:8b:3f:d2:1c:d6:b
10+
run: syntax error: value does not look like a MAC address
11+
- FAILED
12+
13+
IFNAME: bootif0:8b:xz:d2:1c:d6:bc
14+
run: syntax error: value does not look like a MAC address
15+
- FAILED
16+
917
IFNAME: bootif0:8b:3f:d2:1c:d6
10-
run: syntax error: ifname must contain 7 or 21 components
18+
run: syntax error: MAC address must contain 6 or 20 components, but got 5
1119
- FAILED
1220

1321
IFNAME: bootif0:8b:3f:d2:1c:d6:bc:ab
14-
run: syntax error: ifname must contain 7 or 21 components
22+
run: syntax error: MAC address must contain 6 or 20 components, but got 7
23+
- FAILED
24+
25+
IFNAME: bootif0:00:00:10:29:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:99:4c:42:a0
26+
run: syntax error: MAC address must contain 6 or 20 components, but got 21
1527
- FAILED
1628

1729
rc=0

features/network/tests/ts0001-cmdline-ifname/run

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ failed()
2626
(testcase "bootif0:00:00:10:29:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:99:4c:42") || failed
2727

2828
# expect fail
29+
(testcase "bootif0:8b:3f:d2:1c:d6:b") || failed
30+
(testcase "bootif0:8b:xz:d2:1c:d6:bc") || failed
2931
(testcase "bootif0:8b:3f:d2:1c:d6") || failed
3032
(testcase "bootif0:8b:3f:d2:1c:d6:bc:ab") || failed
33+
(testcase "bootif0:00:00:10:29:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:99:4c:42:a0") || failed

features/network/tests/ts0001-cmdline-ip/expect

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ IP: eth0:dhcp:1500:52:54:00:12:34:56
7676
- dns1 :
7777
- dns2 :
7878

79+
IP: eth0:dhcp:1500:00:00:10:29:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:99:4c:42
80+
- ipaddr :
81+
- peer :
82+
- gateway :
83+
- netmask :
84+
- hostname :
85+
- interface : eth0
86+
- autoconf : dhcp
87+
- mtu : 1500
88+
- macaddr : 00:00:10:29:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:99:4c:42
89+
- dns1 :
90+
- dns2 :
91+
92+
IP: eth0:dhcp:1500:00:00:10:29:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:99:4c:xz
93+
run: syntax error: value does not look like a MAC address
94+
- FAILED
95+
7996
IP: 192.168.76.2::192.168.76.1:255.255.255.0:foobar:ether0:none::52:54:00:12:34:56
8097
- ipaddr : 192.168.76.2
8198
- peer :

features/network/tests/ts0001-cmdline-ip/run

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ failed()
3939
(testcase "eth0.3:auto") || failed
4040
(testcase "eth0:dhcp::52:54:00:12:34:56") || failed
4141
(testcase "eth0:dhcp:1500:52:54:00:12:34:56") || failed
42+
(testcase "eth0:dhcp:1500:00:00:10:29:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:99:4c:42") || failed
43+
(testcase "eth0:dhcp:1500:00:00:10:29:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:99:4c:xz") || failed
4244

4345
# format: ip=<ipaddr>:[<peer>]:<gw>:<netmask>:<hostname>:<interface>:<autoconf>[:[<mtu>][:<macaddr>]]
4446
(testcase "192.168.76.2::192.168.76.1:255.255.255.0:foobar:ether0:none::52:54:00:12:34:56") || failed

0 commit comments

Comments
 (0)