From 9ecc11ee5295b6f29f7d32755621b429c9cb4593 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Fri, 9 Nov 2018 21:26:10 +0100 Subject: [PATCH] mescc: Support GNU Bash. * include/errno.h (EACCES, ENOSPC, ESPIPE): New macro. * include/linux/x86/syscall.h (SYS_setuid, SYS_geteuid, SYS_getegid, SYS_setgid): New macro. * include/linux/x86_64/syscall.h: Likewise. * include/stdint.h (LLONG_MIN, LLONG_MAX, SIZE_MAX): New macro. * include/sys/stat.h (S_ISUID, S_ISGID, S_IXGRP, S_IXOTH, S_IRGRP, S_IROTH, S_IWGRP, S_IWOTH, S_IRWXG, S_IRWXO): New macro. * include/termio.h: New file. * include/unistd.h (_POSIX_VERSION): New macro. * lib/linux/gnu.c (geteuid, getegid, setuid, setgid): New function. * lib/string/memchr.c: New file. * lib/stub/getpwuid.c: New file. * lib/stub/rand.c: New file. * lib/stub/ttyname.c: New file. * include/string.h (memchr): Declare. * include/unistd.h (geteuid, getegid, setuid, setgid): Declare. --- include/errno.h | 3 ++ include/linux/x86/syscall.h | 6 ++++ include/linux/x86_64/syscall.h | 6 ++++ include/pwd.h | 3 ++ include/stdint.h | 6 ++++ include/stdlib.h | 1 + include/string.h | 1 + include/sys/stat.h | 11 ++++++ include/termio.h | 64 ++++++++++++++++++++++++++++++++++ include/unistd.h | 8 ++++- lib/libc+gnu.c | 5 +++ lib/linux/gnu.c | 25 +++++++++++++ lib/string/memchr.c | 34 ++++++++++++++++++ lib/stub/getpwuid.c | 33 ++++++++++++++++++ lib/stub/rand.c | 33 ++++++++++++++++++ lib/stub/ttyname.c | 35 +++++++++++++++++++ 16 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 include/termio.h create mode 100644 lib/string/memchr.c create mode 100644 lib/stub/getpwuid.c create mode 100644 lib/stub/rand.c create mode 100644 lib/stub/ttyname.c diff --git a/include/errno.h b/include/errno.h index fe3c78c2..63257384 100644 --- a/include/errno.h +++ b/include/errno.h @@ -44,11 +44,14 @@ int errno; #define ECHILD 10 #define EAGAIN 11 #define ENOMEM 12 +#define EACCES 13 #define EEXIST 17 #define ENOTDIR 20 #define EISDIR 21 #define EINVAL 22 #define EMFILE 24 +#define ENOSPC 28 +#define ESPIPE 29 #define EPIPE 32 #define ERANGE 34 diff --git a/include/linux/x86/syscall.h b/include/linux/x86/syscall.h index e9ed6f36..dd063c82 100644 --- a/include/linux/x86/syscall.h +++ b/include/linux/x86/syscall.h @@ -73,4 +73,10 @@ #define SYS_getdents 0x8d #define SYS_clock_gettime 0x109 +// bash +#define SYS_setuid 0x17 +#define SYS_geteuid 0x31 +#define SYS_getegid 0x32 +#define SYS_setgid 0x3e + #endif // __MES_LINUX_X86_SYSCALL_H diff --git a/include/linux/x86_64/syscall.h b/include/linux/x86_64/syscall.h index ab10bdfb..fddce5c4 100644 --- a/include/linux/x86_64/syscall.h +++ b/include/linux/x86_64/syscall.h @@ -69,4 +69,10 @@ #define SYS_getdents 0x4e #define SYS_clock_gettime 0xe4 +// bash +#define SYS_setuid 0x69 +#define SYS_setgid 0x6a +#define SYS_geteuid 0x6b +#define SYS_getegid 0x6c + #endif // __MES_LINUX_X86_64_SYSCALL_H diff --git a/include/pwd.h b/include/pwd.h index 416d0e9a..f0ce9244 100644 --- a/include/pwd.h +++ b/include/pwd.h @@ -36,6 +36,9 @@ struct passwd char *pw_shell; }; +struct passwd * getpwuid (); + + #endif // ! WITH_GLIBC #endif // __MES_PWD_H diff --git a/include/stdint.h b/include/stdint.h index a7aec03c..e225c7e9 100644 --- a/include/stdint.h +++ b/include/stdint.h @@ -108,11 +108,17 @@ typedef long ptrdiff_t; #define LONG_MAX INT_MAX #define UINT_MAX UINT32_MAX #define ULONG_MAX UINT32_MAX +#define LLONG_MIN INT64_MIN +#define LLONG_MAX INT64_MAX +#define SIZE_MAX UINT32_MAX #elif __x86_64__ #define LONG_MIN INT64_MIN #define LONG_MAX INT64_MAX #define UINT_MAX UINT32_MAX #define ULONG_MAX UINT64_MAX +#define LLONG_MIN INT64_MIN +#define LLONG_MAX INT64_MAX +#define SIZE_MAX UINT64_MAX #endif #endif // ! WITH_GLIBC diff --git a/include/stdlib.h b/include/stdlib.h index a5f94ed3..5d8d62f3 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -42,6 +42,7 @@ int setenv (char const* s, char const* v, int overwrite_p); void unsetenv (char const *name); void *malloc (size_t); void qsort (void *base, size_t nmemb, size_t size, int (*compar)(void const *, void const *)); +int rand (void); void *realloc (void *p, size_t size); double strtod (char const *string, char **tailptr); float strtof (char const *string, char **tailptr); diff --git a/include/string.h b/include/string.h index 450c6235..eb3a6215 100644 --- a/include/string.h +++ b/include/string.h @@ -45,6 +45,7 @@ typedef unsigned long size_t; typedef long ssize_t; #endif +void * memchr (void const *block, int c, size_t size); void *memcpy (void *dest, void const *src, size_t n); void *memmove (void *dest, void const *src, size_t n); void *memset (void *s, int c, size_t n); diff --git a/include/sys/stat.h b/include/sys/stat.h index 17626f41..aab69a9a 100644 --- a/include/sys/stat.h +++ b/include/sys/stat.h @@ -101,6 +101,17 @@ int stat (char const *file_name, struct stat *buf); #define S_IWUSR 00200 #define S_IRUSR 00400 +#define S_ISUID 0400 +#define S_ISGID 02000 +#define S_IXGRP 00010 +#define S_IXOTH 00001 +#define S_IRGRP 00040 +#define S_IROTH 00004 +#define S_IWGRP 00020 +#define S_IWOTH 00002 +#define S_IRWXG 00070 +#define S_IRWXO 00007 + #endif // ! WITH_GLIBC #endif // __MES_SYS_STAT_H diff --git a/include/termio.h b/include/termio.h new file mode 100644 index 00000000..8bd86e05 --- /dev/null +++ b/include/termio.h @@ -0,0 +1,64 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ +#ifndef __MES_TERMIO_H +#define __MES_TERMIO_H 1 + +#if WITH_GLIBC +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#undef __MES_TERMIO_H +#include_next + +#else // ! WITH_GLIBC + +#define TIOCGWINSZ 0x5413 +#define TCGETA 0x5405 +#define TCSETAW 0x5407 + +#define VTIME 5 +#define VMIN 6 + +#define ISIG 0000001 +#define ICANON 0000002 +#define ECHO 0000010 +#define ECHOK 0000040 +#define ECHONL 0000100 + +#define ISTRIP 0000040 +#define INLCR 0000100 +#define ICRNL 0000400 + +#define CS8 0000060 +#define PARENB 0000400 + +struct termio +{ + unsigned short c_iflag; + unsigned short c_oflag; + unsigned short c_cflag; + unsigned short c_lflag; + unsigned char c_line; + unsigned char c_cc[8]; +}; + +#endif // ! WITH_GLIBC + +#endif // __MES_TERMIO_H diff --git a/include/unistd.h b/include/unistd.h index c34584cf..d6c218e4 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -29,6 +29,8 @@ #else // ! WITH_GLIBC +#define _POSIX_VERSION 199009L + #include #ifndef NULL #define NULL 0 @@ -62,8 +64,12 @@ int execve (char const *file, char *const argv[], char *const env[]); int execvp (char const *file, char *const argv[]); int fork (void); char *getcwd (char *buf, size_t size); -gid_t getgid (void); uid_t getuid (void); +gid_t getgid (void); +int setgid (gid_t newgid); +int setuid (uid_t newuid); +uid_t geteuid (void); +gid_t getegid (void); int isatty (int fd); int link (char const *oldname, char const *newname); off_t lseek (int fd, off_t offset, int whence); diff --git a/lib/libc+gnu.c b/lib/libc+gnu.c index 4f997e2f..c2378386 100644 --- a/lib/libc+gnu.c +++ b/lib/libc+gnu.c @@ -113,3 +113,8 @@ #include #include #include + +// bash +#include +#include +#include diff --git a/lib/linux/gnu.c b/lib/linux/gnu.c index 7fc5a8be..bd914d28 100644 --- a/lib/linux/gnu.c +++ b/lib/linux/gnu.c @@ -173,3 +173,28 @@ chdir (char const *file_name) { return _sys_call1 (SYS_chdir, (long)file_name); } + +// bash +uid_t +geteuid () +{ + return _sys_call (SYS_geteuid); +} + +gid_t +getegid () +{ + return _sys_call (SYS_getegid); +} + +int +setuid (uid_t newuid) +{ + return _sys_call1 (SYS_setuid, (long)newuid); +} + +int +setgid (gid_t newgid) +{ + return _sys_call1 (SYS_setgid, (long)newgid); +} diff --git a/lib/string/memchr.c b/lib/string/memchr.c new file mode 100644 index 00000000..eac13a9f --- /dev/null +++ b/lib/string/memchr.c @@ -0,0 +1,34 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include + +void * +memchr (void const *block, int c, size_t size) +{ + char const *p = block; + while (size--) + { + if (c == *p) + return p; + p++; + } + return 0; +} diff --git a/lib/stub/getpwuid.c b/lib/stub/getpwuid.c new file mode 100644 index 00000000..647fd54e --- /dev/null +++ b/lib/stub/getpwuid.c @@ -0,0 +1,33 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include +#include + +struct passwd * +getpwuid () +{ + static int stub = 0; + if (__mes_debug () && !stub) + eputs ("getpwuid stub\n"); + stub = 1; + errno = 0; + return 0; +} diff --git a/lib/stub/rand.c b/lib/stub/rand.c new file mode 100644 index 00000000..2e07282f --- /dev/null +++ b/lib/stub/rand.c @@ -0,0 +1,33 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include +#include + +int +rand (void) +{ + static int stub = 0; + if (__mes_debug () && !stub) + eputs ("rand stub\n"); + stub = 1; + errno = 0; + return 0; +} diff --git a/lib/stub/ttyname.c b/lib/stub/ttyname.c new file mode 100644 index 00000000..093e5c06 --- /dev/null +++ b/lib/stub/ttyname.c @@ -0,0 +1,35 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include +#include + +char * +ttyname (int filedes) +{ + static int stub = 0; + if (__mes_debug () && !stub) + eputs ("ttyname stub\n"); + stub = 1; + errno = 0; + if (isatty (filedes)) + return "/dev/tty0"; + return 0; +}