McdeSys: the Wi-Fi input checks are a function the tests can reach (t0081, a)

The checks that keep a typed network name and password out of
wpa_supplicant.conf and rc.conf (netbsd.c, mcde_sys_wifi_join) sat inline in
the NetBSD branch, behind geteuid() != 0: no test of the build could run them.
They are mcde_sys_wifi_config_ok in net.c now (one implementation for the file
that is built on every system), the NetBSD branch calls it, and the new
test_netinput.c runs 46 checks as an ordinary user: name 1..32 bytes (33
refused), passphrase none or 8..63 (7 and 64 refused), a quote, a backslash or
a newline in either (also a whole injected "network={" block), the interface
name (lower case and digits only), plus the two helpers net.c already had
without a test: mcde_sys_net_ifname_ok and mcde_sys_net_prefix (a
non-contiguous netmask, trailing text).

One behaviour change, found by writing the table: an EMPTY interface name
passed the inline check (strspn("") == strlen("")) and would have given
wpa_supplicant_flags="-B -s -i  -D bsd ..."; it is refused now.

Measured: make check 6 of 6.  Mutation check: with the SSID limit raised to 33,
with the empty-name guard removed, with the passphrase minimum 7, and with the
quote check left out of the passphrase, test_netinput fails (1, 1, 1 and 2
cases); the real code passes.  netbsd.c and net.c compile for NetBSD
(x86_64--netbsd-gcc) without a diagnostic.  Not run on NetBSD.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
lotherkandClaude Sonnet 5.5 committed 2026-11-15 01:24:54 +00:00
1 parent b2dab1ad71
commit 5b2f0f4e8e
5 files changed
+139 -8

No files matched your search

+5 -2
View File
@@ -21,7 +21,7 @@ endif
libMcdeSys_la_LDFLAGS = -version-info 1:0:0
# what the system tells about itself, for a look by hand: make check runs it
check_PROGRAMS = test_sysinfo test_netmount test_updatesource test_processes test_job
check_PROGRAMS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput
test_sysinfo_SOURCES = test_sysinfo.c
test_sysinfo_CPPFLAGS = -I$(top_srcdir)/include
test_sysinfo_LDADD = libMcdeSys.la
@@ -37,4 +37,7 @@ test_processes_LDADD = libMcdeSys.la
test_job_SOURCES = test_job.c
test_job_CPPFLAGS = -I$(top_srcdir)/include
test_job_LDADD = libMcdeSys.la
TESTS = test_sysinfo test_netmount test_updatesource test_processes test_job
test_netinput_SOURCES = test_netinput.c
test_netinput_CPPFLAGS = -I$(top_srcdir)/include
test_netinput_LDADD = libMcdeSys.la
TESTS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput
+30
View File
@@ -119,6 +119,36 @@ mcde_sys_net_ifname_ok(const char *name)
return 1;
}
/* The checks of mcde_sys_wifi_join where the network is written into
* wpa_supplicant.conf (netbsd.c): the interface name is lower-case letters and
* digits (it goes into rc.conf's wpa_supplicant_flags); the network name has 1
* to 32 bytes, the passphrase, if there is one, 8 to 63; neither has a quote,
* a backslash or a newline, which would end the quoted string of the file.
* Kept here, apart from the NetBSD branch, so that make check can run them as
* an ordinary user (the join itself needs the administrator; tasksd t0081).
* 1 if all is well. An empty interface name passed the inline check this
* replaces. */
int
mcde_sys_wifi_config_ok(const char *ifname, const char *ssid,
const char *passphrase)
{
size_t n;
if (ifname == NULL || ifname[0] == '\0' ||
strspn(ifname, "abcdefghijklmnopqrstuvwxyz0123456789") !=
strlen(ifname))
return 0;
if (ssid == NULL || ssid[0] == '\0' || strlen(ssid) > 32 ||
strpbrk(ssid, "\"\n\\") != NULL)
return 0;
if (passphrase != NULL) {
n = strlen(passphrase);
if (n < 8 || n > 63 || strpbrk(passphrase, "\"\n\\") != NULL)
return 0;
}
return 1;
}
/* the prefix length of a dotted netmask, -1 when it is none */
int
mcde_sys_net_prefix(const char *netmask)
+1 -6
View File
@@ -543,12 +543,7 @@ mcde_sys_wifi_join(const char *ifname, const char *ssid,
if (geteuid() != 0)
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
"can join a Wi-Fi network.");
if (ifname == NULL || strspn(ifname, "abcdefghijklmnopqrstuvwxyz"
"0123456789") != strlen(ifname) || ssid == NULL ||
ssid[0] == '\0' || strlen(ssid) > 32 ||
strpbrk(ssid, "\"\n\\") != NULL ||
(passphrase != NULL && (strlen(passphrase) < 8 ||
strlen(passphrase) > 63 || strpbrk(passphrase, "\"\n\\") != NULL)))
if (!mcde_sys_wifi_config_ok(ifname, ssid, passphrase))
return mcde_sys_fail(MCDE_SYS_EINVAL, "The network name or the "
"password cannot be used (a password has 8 to 63 "
"characters, no quotes).");
+4
View File
@@ -55,6 +55,10 @@ McdeSysNetKind mcde_sys_if_kind(const char *name, char *ssid, size_t len);
/* net.c: an interface name that may go on a command line; the prefix
* length of a dotted netmask (-1: none) and the netmask of a prefix */
int mcde_sys_net_ifname_ok(const char *name);
/* net.c: may this network go into wpa_supplicant.conf (the checks of the
* NetBSD mcde_sys_wifi_join); 1 if so */
int mcde_sys_wifi_config_ok(const char *ifname, const char *ssid,
const char *passphrase);
int mcde_sys_net_prefix(const char *netmask);
void mcde_sys_net_mask(int prefix, char *buf, size_t len);
/* the backend: the configured settings of IFNAME, its router, Ethernet ID
+99
View File
@@ -0,0 +1,99 @@
/*
* test_netinput.c -- McdeSys: what may reach a network command or a file (make check)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The checks that stand between a typed network name or password and
* wpa_supplicant.conf, rc.conf and the command lines of the network tools
* (review 2026-11-13, tasksd t0081): the Wi-Fi network (a name of 33 bytes, a
* quote, a backslash or a newline would end the quoted string of the file or
* add a line to it), the interface name, the netmask.
*/
#include <stdio.h>
#include <string.h>
#include <Mcde/McdeSys.h>
#include "private.h" /* the library's checks, not the API */
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
int
main(void)
{
char s32[33], s33[34], p63[64], p64[65], p7[8], p8[9];
memset(s32, 'n', 32); s32[32] = '\0';
memset(s33, 'n', 33); s33[33] = '\0';
memset(p63, 'p', 63); p63[63] = '\0';
memset(p64, 'p', 64); p64[64] = '\0';
memset(p7, 'p', 7); p7[7] = '\0';
memset(p8, 'p', 8); p8[8] = '\0';
/* the network: name 1..32 bytes, passphrase none or 8..63 */
CHECK(mcde_sys_wifi_config_ok("iwn0", "Home", "hunter22"));
CHECK(mcde_sys_wifi_config_ok("wlan0", "Home Network", NULL)); /* open */
CHECK(mcde_sys_wifi_config_ok("iwn0", "caf\xc3\xa9", "pass word"));
CHECK(mcde_sys_wifi_config_ok("iwn0", s32, p63)); /* the longest */
CHECK(mcde_sys_wifi_config_ok("iwn0", "a", p8)); /* the shortest */
CHECK(!mcde_sys_wifi_config_ok("iwn0", s33, p8)); /* name 33 bytes */
CHECK(!mcde_sys_wifi_config_ok("iwn0", "", p8)); /* no name */
CHECK(!mcde_sys_wifi_config_ok("iwn0", NULL, p8));
CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", p7)); /* 7 */
CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", p64)); /* 64 */
CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", ""));
/* what would end the quoted string or add a line to the file */
CHECK(!mcde_sys_wifi_config_ok("iwn0", "a\"b", p8));
CHECK(!mcde_sys_wifi_config_ok("iwn0", "a\\b", p8));
CHECK(!mcde_sys_wifi_config_ok("iwn0", "a\nb", p8));
CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home\"\n}\nnetwork={ssid=\"x", p8));
CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", "pass\"word1"));
CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", "pass\\word1"));
CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", "pass\nword1"));
CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home",
"12345678\"\n\tkey_mgmt=NONE"));
/* the interface name goes into rc.conf: lower case and digits only */
CHECK(mcde_sys_wifi_config_ok("urtwn0", "Home", p8));
CHECK(!mcde_sys_wifi_config_ok(NULL, "Home", p8));
CHECK(!mcde_sys_wifi_config_ok("", "Home", p8)); /* was accepted */
CHECK(!mcde_sys_wifi_config_ok("iwn0 -x", "Home", p8));
CHECK(!mcde_sys_wifi_config_ok("iwn0;id", "Home", p8));
CHECK(!mcde_sys_wifi_config_ok("-iwn0", "Home", p8));
CHECK(!mcde_sys_wifi_config_ok("IWN0", "Home", p8));
CHECK(!mcde_sys_wifi_config_ok("iwn0\"", "Home", p8));
/* an interface name for a command line (the settings pane) */
CHECK(mcde_sys_net_ifname_ok("iwn0"));
CHECK(mcde_sys_net_ifname_ok("eth0.100"));
CHECK(mcde_sys_net_ifname_ok("br-lan"));
CHECK(!mcde_sys_net_ifname_ok(NULL));
CHECK(!mcde_sys_net_ifname_ok(""));
CHECK(!mcde_sys_net_ifname_ok("-i"));
CHECK(!mcde_sys_net_ifname_ok("eth0 x"));
CHECK(!mcde_sys_net_ifname_ok("eth0;id"));
CHECK(!mcde_sys_net_ifname_ok("a\nb"));
CHECK(!mcde_sys_net_ifname_ok("0123456789abcdef")); /* IFNAMSIZ */
/* a netmask: contiguous ones only */
CHECK(mcde_sys_net_prefix("255.255.255.0") == 24);
CHECK(mcde_sys_net_prefix("255.255.255.255") == 32);
CHECK(mcde_sys_net_prefix("255.255.254.0") == 23);
CHECK(mcde_sys_net_prefix("0.0.0.0") == 0);
CHECK(mcde_sys_net_prefix("255.0.255.0") == -1);
CHECK(mcde_sys_net_prefix("255.255.255.1") == -1);
CHECK(mcde_sys_net_prefix("24") == -1);
CHECK(mcde_sys_net_prefix("255.255.255.0; id") == -1);
CHECK(mcde_sys_net_prefix("") == -1);
if (failures == 0)
printf("test_netinput: all passed\n");
return failures != 0;
}