Files
lotherkandClaude Sonnet 5.5 378a6bc805 McdeSys: an AFP password no longer shows in ps(1) (t0077, A)
mcde_sys_net_mount built afp://user:PASSWORD@host/vol and that was argv[1] of
mount_afp: everyone on the computer could read it with ps(1) for as long as the
mount ran (measured with a fake mount_afp and a slow tool: "mount_afp
afp://alice:TopSecret-42@nas.local/Volume /tmp/mp-test").  The address says
"-" now, and the password is one line of the tool's standard input:
afpfs-ng's mount_afp asks getpass(3) for a password "-" (fuse/client.c), and
getpass reads the standard input where the process has no controlling terminal
(src/lib/libc/gen/getpass.c:106-112: "Try to use /dev/tty if possible;
otherwise read from stdin").  So no change in the fork.

For that the tool must have no controlling terminal even when the program was
started from a terminal: the job runner is a session of its own now (setsid
instead of setpgid; the group is still the runner's, cancelling still ends the
tool; kill of the pid too, for the moment before the setsid), and
mcde_sys_net_mount is _start plus waiting, so it detaches the same way.  The
restriction against "@", "/" and ":" in an AFP password is gone (they were in the
address); a line break or more than 297 characters is refused for sshfs and
mount_afp alike.

Measured with the fake mount_afp (records argv, whether /dev/tty opens, what
getpass returns): argv[1] afp://alice:-@nas.local/Volume, getpass returned the
password, also "pa:ss@w/rd 1"; under a pseudo-terminal the OLD code kept
"controlling terminal present" and the new one has "no controlling terminal"
and still gets the password; no password -> afp://alice@nas.local/Volume, no
getpass; a line break -> refused.  ps during a mount: old code showed the
password in mount_afp's line, new code "-".  test_netmount: 6 cases of
mcde_sys_net_afp_address (a function that is not given the password, so it
cannot put it into the address); make check 6 of 6 (test_job's cancel test still
passes); the dtfile timeout/Escape scenario still leaves 0 tool processes.

Not done of t0077: B (the /tmp/afp_server-<uid> socket and /tmp/fuse_stderr of
afpfs-ng), and the real mount_afp against a netatalk server (the fake stands
in; only the standard-input and argv behaviour is measured).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 04:45:29 +00:00

169 lines
5.0 KiB
C

/*
* test_netmount.c -- McdeSys: which host names reach the list (make check)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* mcde_sys_net_add() is where a name from the network (mDNS, a browse) is
* checked once for every program that opens it: a host beginning with "-"
* would reach ssh or a mount tool as an option, a quote or a blank would
* reach a shell (review 2026-11-13, tasksd t0076).
*/
#include <stdio.h>
#include <string.h>
#include <Mcde/McdeSys.h>
#include "private.h" /* mcde_sys_net_add: the library's, not the API */
static const struct {
const char *host;
int kept;
} cases[] = {
{ "kitt.local.", 1 }, /* the trailing dot goes */
{ "kitt.local", 1 },
{ "fe80::1", 1 }, /* IPv6 */
{ "-oProxyCommand=touch${IFS}/tmp/x.local", 0 },
{ "-l", 0 },
{ "host;id", 0 },
{ "host name", 0 },
{ "it's", 0 },
{ "a\nb", 0 },
{ "$(id)", 0 },
{ "", 0 },
{ ".", 0 }, /* empty after the dot */
};
/* the address a person types (or a favourite holds): what may reach sshfs,
* mount_nfs and mount_afp as an argument (review 2026-11-13, tasksd t0081).
* The sftp tool gets "USER@HOST:PATH" as its first argument, so a USER that
* starts with "-" would be an option. */
static const struct {
const char *url;
int ok;
} urls[] = {
{ "sftp://host/", 1 },
{ "sftp://user@host/path", 1 },
{ "sftp://u.ser-1_x@10.0.0.1/p/q", 1 },
{ "nfs://server.local/export", 1 },
{ "afp://user@host/Volume", 1 },
{ "sftp://fe80::1/x", 1 }, /* IPv6 written bare */
{ "sftp://@host/", 1 }, /* an empty user: none */
{ "sftp://-oProxyCommand=id/", 0 }, /* a host that is an option */
{ "sftp://user@-host/", 0 },
{ "sftp://-l@host/", 0 }, /* a user that is an option */
{ "sftp://-p22@host/", 0 },
{ "sftp://us er@host/", 0 },
{ "sftp://user;id@host/", 0 },
{ "sftp://user@host name/", 0 },
{ "sftp://host/a\nb", 0 }, /* a newline in the path */
{ "sftp:///path", 0 }, /* no host */
{ "sftp://", 0 },
{ "host/path", 0 }, /* no scheme */
{ "abcdefghijklmnop://host/", 0 }, /* a scheme of 16 characters */
{ "sftp://user@host@evil/", 0 },
{ "sftp://host$(id)/", 0 },
{ "", 0 },
};
int
main(void)
{
McdeSysNetHost hosts[4];
size_t i;
int failures = 0, n;
for (i = 0; i < sizeof urls / sizeof urls[0]; i++) {
n = mcde_sys_net_url_ok(urls[i].url) != 0;
if (n != urls[i].ok) {
printf("FAIL url \"%s\": %s\n", urls[i].url,
n ? "accepted" : "refused");
failures++;
} else
printf("ok url \"%s\": %s\n", urls[i].url,
n ? "accepted" : "refused");
}
/* a host of 256 characters and one of 255 */
{
char long_url[320];
memset(long_url, 0, sizeof long_url);
strcpy(long_url, "sftp://");
memset(long_url + 7, 'a', 256);
strcat(long_url, "/");
if (mcde_sys_net_url_ok(long_url)) {
printf("FAIL a host of 256 characters accepted\n");
failures++;
}
long_url[7 + 255] = '\0';
strcat(long_url, "/");
if (!mcde_sys_net_url_ok(long_url)) {
printf("FAIL a host of 255 characters refused\n");
failures++;
}
}
if (mcde_sys_net_url_ok(NULL)) {
printf("FAIL NULL accepted\n");
failures++;
}
/* the address mount_afp gets as argv[1] shows in ps(1): "-" stands where
* the password would be (mount_afp asks getpass(3) for it) */
{
static const struct {
const char *url;
int pw;
const char *want;
} afp[] = {
{ "afp://alice@nas.local/Volume", 1,
"afp://alice:-@nas.local/Volume" },
{ "afp://alice@nas.local/Volume", 0,
"afp://alice@nas.local/Volume" },
{ "afp://nas.local/Volume", 0, "afp://nas.local/Volume" },
{ "afp://nas.local/Volume", 1, "afp://:-@nas.local/Volume" },
{ "afp://alice@nas.local", 1, "afp://alice:-@nas.local" },
{ "afp://u.s-e_r@10.0.0.7/a/b", 1,
"afp://u.s-e_r:-@10.0.0.7/a/b" },
};
char buf[1024];
for (i = 0; i < sizeof afp / sizeof afp[0]; i++) {
n = mcde_sys_net_afp_address(afp[i].url, afp[i].pw, buf,
sizeof buf);
if (!n || strcmp(buf, afp[i].want) != 0) {
printf("FAIL afp address of \"%s\" (%d): "
"\"%s\"\n", afp[i].url, afp[i].pw,
n ? buf : "(refused)");
failures++;
} else
printf("ok afp address \"%s\"\n", buf);
}
if (mcde_sys_net_afp_address("sftp://-x@host/", 1, buf,
sizeof buf) || mcde_sys_net_afp_address(NULL, 1, buf,
sizeof buf)) {
printf("FAIL an address that is none got an AFP "
"address\n");
failures++;
}
}
for (i = 0; i < sizeof cases / sizeof cases[0]; i++) {
n = mcde_sys_net_add(hosts, 0, 4, "name", cases[i].host,
MCDE_NET_HAS_SSH);
if (n != cases[i].kept) {
printf("FAIL \"%s\": %s\n", cases[i].host,
n ? "kept" : "dropped");
failures++;
} else
printf("ok \"%s\": %s\n", cases[i].host,
n ? "kept" : "dropped");
}
/* the service name is free text: no control characters on screen */
n = mcde_sys_net_add(hosts, 0, 4, "evil\033[2Jname", "kitt.local",
MCDE_NET_HAS_SSH);
if (n != 1 || strchr(hosts[0].name, '\033') != NULL) {
printf("FAIL control character in the name\n");
failures++;
}
return failures != 0;
}