Add support for `getrandom`. And a simple test. Bug: linux-syscall-support:31 Change-Id: Ifea152cdea89183f69d881807f3bd60da56a4513 Reviewed-on: https://chromium-review.googlesource.com/c/linux-syscall-support/+/2351965 Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/linux_syscall_support.h b/linux_syscall_support.h index a087903..e4ac226 100644 --- a/linux_syscall_support.h +++ b/linux_syscall_support.h
@@ -954,6 +954,9 @@ #ifndef __NR_fallocate #define __NR_fallocate 324 #endif +#ifndef __NR_getrandom +#define __NR_getrandom 355 +#endif /* End of i386 definitions */ #elif defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) #ifndef __NR_setresuid @@ -1058,6 +1061,9 @@ #ifndef __NR_getcpu #define __NR_getcpu (__NR_SYSCALL_BASE + 345) #endif +#ifndef __NR_getrandom +#define __NR_getrandom (__NR_SYSCALL_BASE + 384) +#endif /* End of ARM 3/EABI definitions */ #elif defined(__aarch64__) #ifndef __NR_setxattr @@ -1155,6 +1161,9 @@ #ifndef __NR_move_pages #define __NR_move_pages 239 #endif +#ifndef __NR_getrandom +#define __NR_getrandom 278 +#endif /* End of aarch64 definitions */ #elif defined(__x86_64__) #ifndef __NR_pread64 @@ -1246,6 +1255,9 @@ #ifndef __NR_fallocate #define __NR_fallocate 285 #endif +#ifndef __NR_getrandom +#define __NR_getrandom 318 +#endif /* End of x86-64 definitions */ #elif defined(__mips__) #if _MIPS_SIM == _MIPS_SIM_ABI32 @@ -1347,6 +1359,9 @@ #ifndef __NR_ioprio_get #define __NR_ioprio_get (__NR_Linux + 315) #endif +#ifndef __NR_getrandom +#define __NR_getrandom (__NR_Linux + 353) +#endif /* End of MIPS (old 32bit API) definitions */ #elif _MIPS_SIM == _MIPS_SIM_ABI64 #ifndef __NR_pread64 @@ -1425,6 +1440,9 @@ #ifndef __NR_ioprio_get #define __NR_ioprio_get (__NR_Linux + 274) #endif +#ifndef __NT_getrandom +#define (__NR_Linux + 313) +#endif /* End of MIPS (64bit API) definitions */ #else #ifndef __NR_setresuid @@ -3658,6 +3676,10 @@ int, f, int, mode, loff_t, offset, loff_t, len) #endif #endif + #if defined(__NR_getrandom) + LSS_INLINE _syscall3(ssize_t, getrandom, void*, buffer, size_t, length, + unsigned int, flags) + #endif #if defined(__NR_newfstatat) LSS_INLINE _syscall4(int, newfstatat, int, d, const char *, p,
diff --git a/tests/Makefile b/tests/Makefile index bcf7e1f..60ec06e 100644 --- a/tests/Makefile +++ b/tests/Makefile
@@ -42,6 +42,7 @@ TESTS = \ fallocate \ sigaction \ + getrandom \ sigtimedwait \ unlink \
diff --git a/tests/getrandom.c b/tests/getrandom.c new file mode 100644 index 0000000..eb1f162 --- /dev/null +++ b/tests/getrandom.c
@@ -0,0 +1,59 @@ +/* Copyright 2020, Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "test_skel.h" + +#define BUFFER_SIZE 256 + +int main(int argc, char *argv[]) { + char buffer[BUFFER_SIZE]; + // Zero it out so we can check later that it's at least not all 0s. + memset(buffer, 0, BUFFER_SIZE); + bool buffer_contains_all_zeros = true; + + // Don't bother passing any flags. (If we're using lss, we might not have the + // right header files with the flags defined anyway, and we'd have to copy + // this in here too, and risk getting out of sync in yet another way.) + const ssize_t r = sys_getrandom(buffer, BUFFER_SIZE, 0); + + // Make sure it either worked, or that it's just not supported. + assert(r == BUFFER_SIZE || errno == ENOSYS); + + if (r == BUFFER_SIZE) { + // If all the bytes are 0, it didn't really work. + for (size_t i = 0; i < BUFFER_SIZE; ++i) { + if (buffer[i] != 0) { + buffer_contains_all_zeros = false; + } + } + assert(!buffer_contains_all_zeros); + } + + return 0; +}
diff --git a/tests/test_skel.h b/tests/test_skel.h index 9ff0eb3..13d8bea 100644 --- a/tests/test_skel.h +++ b/tests/test_skel.h
@@ -44,9 +44,11 @@ #include <assert.h> #include <fcntl.h> #include <sched.h> +#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <sys/mman.h> #include <sys/prctl.h> #include <sys/stat.h>