Restrict systems operations on OpenBSD

Use pledge(2)[0] to limit jq(1) to reading files.
It does not change files and only writes to standard output/error.
It never deals with TTY, network, process management or other subsystems.

This is to reduce jq's attack surface and potential damage.

OpenBSD is carrying a local patch[1] in its official jq port/package
since 2016.  An improved version:

- drop no longer needed "getpw" promise
  f1c4947 "Avoid getpwuid for static linking" removed getpwuid(3) usage
- pledge before jq_init() to simplify the error path
- use perror(3) to print errno(2)

No behaviour change in tests or real world usage observed on
OpenBSD/amd64 7.4.

0: https://man.openbsd.org/pledge.2
1: https://cvsweb.openbsd.org/cgi-bin/cvsweb/ports/textproc/jq/patches/patch-main_c
This commit is contained in:
Klemens Nanni authored and Nico Williams committed 2023-10-21 23:30:36 -05:00
1 parent 9de0e26ce6
commit f1bfd0c518
1 file changed
+7
+7
View File
@@ -324,6 +324,13 @@ int main(int argc, char* argv[]) {
(void) setlocale(LC_ALL, "");
#endif
#ifdef __OpenBSD__
if (pledge("stdio rpath", NULL) == -1) {
perror("pledge");
exit(JQ_ERROR_SYSTEM);
}
#endif
#ifdef WIN32
jv_tsd_dtoa_ctx_init();
fflush(stdout);