From f8a81c329d15f8149b66861f020c9a92b1d63722 Mon Sep 17 00:00:00 2001 From: Nick Briggs Date: Mon, 23 Apr 2018 16:56:12 -0700 Subject: [PATCH] Correct preloading of Unix username into buffer on byte-swapped system. Correct potential memory smash if username exceeds allocated space. modified: ../src/initsout.c --- src/initsout.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/initsout.c b/src/initsout.c index 0f20b3d..96c604d 100644 --- a/src/initsout.c +++ b/src/initsout.c @@ -160,20 +160,30 @@ void init_ifpage(int sysout_size) { /* get user name and stuff into vmem; this is the VMEM buffer; This is a BCPL string -- it starts with a length count. C strings are null terminated instead */ + InterfacePage->usernameaddr = 0; #ifndef DOS { struct passwd *pwd; - char *s = (char *)Addr68k_from_LADDR(0155001); - /* try getpwuid first; use cuserid if it fails */ - if ((pwd = getpwuid(getuid())) == NULL) -#if defined(MACOSX) || defined(FREEBSD) - ; -#else - cuserid(s + 1); + char *s; + int len; + /* Get username from getpwuid */ + /* The page/offset we are using is hardcoded in LLFAULT in functions */ + /* \MAIKO.NEWFAULTINIT and \MAIKO.ASSIGNBUFFERS */ + if ((pwd = getpwuid(getuid())) != NULL) { + InterfacePage->usernameaddr = 0155001; + s = (char *)Addr68k_from_LADDR(InterfacePage->usernameaddr); + len = strlen(pwd->pw_name); + /* Lisp reserves 32 words for the BCPL String */ + len = (len < 32 * BYTESPER_DLWORD) ? len : 32 * BYTESPER_DLWORD - 1; + *s = (char)len; + strncpy(s + 1, pwd->pw_name, len); +#ifdef BYTESWAP + /* we must swap the area we have written into, starting at 0155000 */ + /* rounding up to 4-byte words */ + word_swap_page(Addr68k_from_LADDR(0155000), (len + 1 + 2 + 3) / 4); #endif - else - strcpy(s + 1, pwd->pw_name); - *s = (char)strlen(s + 1); + } + } #endif /* DOS */