feat(chvt): use custom implementation instead of system()

Calling system() is vulnerable and shouldn't be used.
This commit is contained in:
grialion 2024-08-06 17:11:18 +02:00
parent eeba4a5f39
commit e745b27a68
4 changed files with 53 additions and 9 deletions

View File

@ -10,10 +10,10 @@ ALLFLAGS=$(CFLAGS) -I$(IDIR)
LIBS=-lm -lpam -lpam_misc LIBS=-lm -lpam -lpam_misc
_DEPS = util.h ui.h config.h auth.h efield.h keys.h users.h sessions.h _DEPS = util.h ui.h config.h auth.h efield.h keys.h users.h sessions.h chvt.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS)) DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = main.o util.o ui.o config.o auth.o efield.o users.o sessions.o _OBJ = main.o util.o ui.o config.o auth.o efield.o users.o sessions.o chvt.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: $(CDIR)/%.c $(DEPS) $(ODIR)/%.o: $(CDIR)/%.c $(DEPS)

21
include/chvt.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef _CHVTH_
#define _CHVTH_
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define KDGKBTYPE 0x4b33
#define VT_ACTIVATE 0x5606
#define VT_WAITACTIVE 0x5607
/**
* @brief change foreground virtual terminal to `n`
*
* @param n virtual terminal number
* @return int non-negative value on success
*/
int chvt(int n);
#endif

22
src/chvt.c Normal file
View File

@ -0,0 +1,22 @@
#include "chvt.h"
static char *vterms[] = {"/dev/tty", "/dev/tty0", "/dev/vc/0", "/dev/systty",
"/dev/console"};
int chvt(int n) {
char c = 0;
for (int i = 0; i < sizeof(vterms) / sizeof(vterms[0]); i++) {
int fd = open(vterms[i], O_RDWR);
if (fd >= 0 && isatty(fd) && ioctl(fd, KDGKBTYPE, &c) == 0 && c < 3) {
if (ioctl(fd, VT_ACTIVATE, n) < 0 || ioctl(fd, VT_WAITACTIVE, n) < 0) {
printf("Couldn't active vt %d\n", n);
return -1;
}
return 0;
}
close(fd);
}
printf("Couldn't get a file descriptor referring to the console.\n");
return -1;
}

View File

@ -4,22 +4,23 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <chvt.h>
#include <config.h> #include <config.h>
#include <sessions.h> #include <sessions.h>
#include <ui.h> #include <ui.h>
#include <users.h> #include <users.h>
void chvt(char *arg) { int chvt_char(char c) {
size_t bsize = snprintf(NULL, 0, "chvt %s", arg) + 1; int i = c - '0';
char *buf = malloc(bsize); if (i >= 0 && i <= 9) {
snprintf(buf, bsize, "chvt %s", arg); return chvt(i);
system(buf); }
free(buf); return -1;
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc == 2) if (argc == 2)
chvt(argv[1]); chvt_char(argv[1][0]);
struct config *config = parse_config("/etc/lidm.ini"); struct config *config = parse_config("/etc/lidm.ini");
if (config == NULL) { if (config == NULL) {