├── CMakeLists.txt ├── COPYING ├── README.md ├── config.h.in ├── example ├── openbsd-example.lua ├── unveil.lua └── userokay.lua └── src └── lua-openbsd.c /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.2) 2 | PROJECT(lua-openbsd) 3 | 4 | FIND_PACKAGE(PkgConfig) 5 | INCLUDE(CheckFunctionExists) 6 | 7 | SET(MODLUA_VERSION "5.1" CACHE STRING "MODLUA_VERSION from ports tree.") 8 | STRING(REPLACE "." "" LUA_PKGCONFIG_VERSION ${MODLUA_VERSION}) 9 | 10 | PKG_CHECK_MODULES(LUA REQUIRED "lua${LUA_PKGCONFIG_VERSION}") 11 | 12 | ADD_DEFINITIONS("-Wall -Werror") 13 | 14 | CHECK_FUNCTION_EXISTS("pledge" HAVE_PLEDGE) 15 | CHECK_FUNCTION_EXISTS("arc4random" HAVE_ARC4RANDOM) 16 | CHECK_FUNCTION_EXISTS("arc4random_uniform" HAVE_ARC4RANDOM_UNIFORM) 17 | CHECK_FUNCTION_EXISTS("unveil" HAVE_UNVEIL) 18 | CHECK_FUNCTION_EXISTS("auth_userokay" HAVE_AUTH_USEROKAY) 19 | CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/config.h.in" 20 | "${CMAKE_BINARY_DIR}/config.h") 21 | 22 | 23 | SET(SOURCES "src/lua-openbsd.c") 24 | SET(HEADERS "${CMAKE_BINARY_DIR}/config.h") 25 | 26 | INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR} 27 | ${LUA_INCLUDE_DIRS}) 28 | 29 | SET(TARGET "openbsd") 30 | 31 | ADD_LIBRARY(${TARGET} SHARED ${SOURCES} ${HEADERS}) 32 | SET_TARGET_PROPERTIES(${TARGET} PROPERTIES PREFIX "") 33 | TARGET_LINK_LIBRARIES(${TARGET} ${LUA_LIBRARIES} ${LUA_LDFLAGS}) 34 | 35 | INSTALL(TARGETS ${TARGET} 36 | DESTINATION /usr/local/lib/lua/${MODLUA_VERSION}/) 37 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Florian Stinglmayr 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenBSD Lua Library 2 | 3 | Implements: 4 | 5 | * pledge() 6 | * arc4random() 7 | * arc4random_uniform() 8 | * unveil() 9 | * auth_userokay() 10 | 11 | Works and has been tested on Lua 5.1, 5.2, 5.3 and 5.4. 12 | 13 | Note that pledge() pre 6.3 takes an optional set of paths as the second 14 | argument. 15 | 16 | ```lua 17 | -- OK 18 | o.pledge('rpath stdio') 19 | -- Error 20 | o.pledge('rpath stdio', 'stdio') 21 | ``` 22 | 23 | ## Build 24 | 25 | Don't forget to set the `MODLUA_VERSION` variable to the Lua version you are 26 | using. 27 | 28 | ``` 29 | $ mkdir build && cd build 30 | $ cmake .. -DMODLUA_VERSION=5.4 31 | $ make 32 | $ make install 33 | ``` 34 | 35 | ## Usage 36 | 37 | ```lua 38 | #!/usr/bin/env lua51 39 | 40 | o = require('openbsd') 41 | ret, error_string = o.pledge('rpath stdio') 42 | if ret == -1 then 43 | error(error_string) 44 | end 45 | ``` 46 | 47 | Or: 48 | 49 | ``` 50 | #!/usr/bin/env lua54 51 | 52 | o = require('openbsd') 53 | 54 | for i = 1, 10 do 55 | print(o.arc4random_uniform(20)) 56 | end 57 | ``` 58 | 59 | ## Author 60 | 61 | Written by Florian Stinglmayr 62 | Copyright (c) 2016, All Rights Reserved 63 | -------------------------------------------------------------------------------- /config.h.in: -------------------------------------------------------------------------------- 1 | #ifndef LUA_PLEDGE_CONFIG_H 2 | #define LUA_PLEDGE_CONFIG_H 3 | 4 | /* Do we have the pledge() system call? 5 | */ 6 | #cmakedefine HAVE_PLEDGE 7 | 8 | /* Do we have arc4random()? 9 | */ 10 | #cmakedefine HAVE_ARC4RANDOM 11 | 12 | /* Do we have arc4random_uniform()? 13 | */ 14 | #cmakedefine HAVE_ARC4RANDOM_UNIFORM 15 | 16 | /* Do we have unveil()? 17 | */ 18 | #cmakedefine HAVE_UNVEIL 19 | 20 | /* Do we have auth_userokay()? 21 | */ 22 | #cmakedefine HAVE_AUTH_USEROKAY 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /example/openbsd-example.lua: -------------------------------------------------------------------------------- 1 | o = require("openbsd") 2 | 3 | -- Test out unveil 4 | ret, errstr = o.unveil(".", "rwx") 5 | print("unveil:", ret, errstr) 6 | 7 | -- Same as pledge("rpath", NULL) 8 | ret, s = o.pledge("rpath stdio") 9 | print("pledge:", ret, s) 10 | 11 | -- Same as pledge("rpath stdio wpath", "rpath stdio") 12 | ret, s = o.pledge("rpath stdio wpath", "rpath stdio") 13 | print("pledge:", ret, s) 14 | 15 | print(o.arc4random()) 16 | 17 | for i = 1, 10 do 18 | print(o.arc4random_uniform(10)) 19 | end 20 | -------------------------------------------------------------------------------- /example/unveil.lua: -------------------------------------------------------------------------------- 1 | o = require("openbsd") 2 | 3 | print("adding /tmp ...") 4 | print(o.unveil("/tmp", "rw")) 5 | print("adding /var/cache ...") 6 | print(o.unveil("/var/cache", "r")) 7 | 8 | -- Remove further calls to unveil 9 | print("disabling further calls to unveil ...") 10 | print(o.unveil(nil, nil)) 11 | 12 | -- Check if it worked 13 | print("adding /etc (should fail) ...") 14 | print(o.unveil("/etc", "rwx")) 15 | 16 | print("testing arg check (should fail) ...") 17 | status, err = pcall(o.unveil, false, 1.0) 18 | print(status, err) 19 | -------------------------------------------------------------------------------- /example/userokay.lua: -------------------------------------------------------------------------------- 1 | o = require("openbsd") 2 | 3 | -- lua userokay.lua