DragonFlyBSD Kernel Audit
DF-2896 / gate_test.c
← back to finding ↓ download raw
/*
 * UCONSOLE gate test: can an unprivileged user set constty via TIOCCONS
 * on their own pty?  (Default X86_64_GENERIC has options UCONSOLE.)
 *
 * expected: ioctl returns 0 for the unpriv user.
 */
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>

int
main(void)
{
	int m, s, on = 1;
	char name[64];

	m = open("/dev/ptmx", O_RDWR);
	if (m < 0) { perror("open /dev/ptmx"); exit(1); }
	if (grantpt(m) || unlockpt(m)) { perror("grantpt/unlockpt"); exit(1); }
	{
		char *pn = ptsname(m);
		if (pn == NULL) { perror("ptsname"); exit(1); }
		snprintf(name, sizeof(name), "%s", pn);
	}
	s = open(name, O_RDWR);
	if (s < 0) { perror("open pts"); exit(1); }
	if (ioctl(s, TIOCCONS, &on) < 0) {
		perror("TIOCCONS");
		printf("GATE: TIOCCONS FAILED for uid=%d\n", getuid());
		exit(2);
	}
	printf("GATE: TIOCCONS OK for uid=%d (constty now our pty)\n", getuid());
	/* clear it again to not disturb the system */
	on = 0;
	ioctl(s, TIOCCONS, &on);
	return (0);
}