From 13ff8745c9dafcff23e549b5cf6505ebe9a6855e Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Tue, 1 Mar 2022 19:32:03 -0800 Subject: [PATCH] Update OpenExecutable to check for result from open with RDWR access --- libc/runtime/openexecutable.S | 12 ++++++++++++ tool/net/redbean.c | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/libc/runtime/openexecutable.S b/libc/runtime/openexecutable.S index c76f03139..7efdcd9c3 100644 --- a/libc/runtime/openexecutable.S +++ b/libc/runtime/openexecutable.S @@ -41,6 +41,7 @@ OpenExecutable: pushq MAP_PRIVATE(%rip) # -0x30(%rbp) pushq MAP_FIXED(%rip) # -0x38(%rbp) pushq __NR_mprotect(%rip) # -0x40(%rbp) + pushq O_RDONLY(%rip) # -0x48(%rbp) push %rbx # code buffer push %r12 # data buffer push %r14 # filename @@ -122,6 +123,17 @@ OpenExecutable: mov -0x20(%rbp),%esi # O_RDWR syscall mov %eax,%r15d + mov $-1,%eax # check if successful + cmp %eax,%r15d + jne 0f + +// Open executable in read-only mode. + mov -0x08(%rbp),%eax # __NR_open + mov %r14,%rdi + mov -0x48(%rbp),%esi # O_RDONLY + syscall + mov %eax,%r15d +0: // Map code segment. mov -0x10(%rbp),%eax # __NR_mmap diff --git a/tool/net/redbean.c b/tool/net/redbean.c index a929286f9..40ed300ad 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -6854,8 +6854,8 @@ static void RestoreApe(void) { if (endswith(zpath, ".com.dbg")) return; if ((a = GetAssetZip("/.ape", 5)) && (p = LoadAsset(a, &n))) { close(zfd); - zfd = OpenExecutable(); - write(zfd, p, n); + if ((zfd = OpenExecutable()) == -1 || write(zfd, p, n) == -1) + WARNF("(srvr) can't restore .ape"); free(p); } else { WARNF("(srvr) /.ape not found");