-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
config: try to parse as ProtoJSON #233
Conversation
Could we 'simply' accept both? if (parseJson(...) == false) { Sth a'la (just better with logging):
|
I wondered this, but I'm not familiar with all the intricacies of TextProto and ProtoJSON to know there aren't atypical config files that could parse as valid TextProto and ProtoJSON, and thus a fallback-parser would result in confusing semantics. But, I'd guess any such atypical files were maliciously crafted. Meanwhile, I guess nsjail assumes the config file is trusted (else all bets are off). I've updated this to PR to your suggestion (updated to only log if both parsers fail to recognize the file). |
JSON is an easier to generate than TextProto in some environments, e.g. Jsonnet or Nix. File I/O had to be reworked to support the json_util API: https://protobuf.dev/reference/cpp/api-docs/google.protobuf.util.json_util/
Here's some test cases with the improved code. Setup (to workaround NixOS not adhering to Linux FHS):
Test casesDegenerate
✅
✅. This was initially surprising to me, but I see what's happened. An empty file parses as valid TextProto, and is then passed to parseInternal. Alas, there is no JSON
✅ Nearly valid JSON: $ sed -i 's/true/tru/' busybox-sh.json
$ ./nsjail -C busybox-sh.json
[W][2024-06-25T22:59:03+1000][18069] parseFile():346 Config file 'busybox-sh.json' failed to parse as either TextProto or ProtoJSON
[W][2024-06-25T22:59:03+1000][18069] flushLog():316 config.cc: 'Error parsing text-format nsjail.NsJailConfig: 1:1: Expected identifier, got: {'
[W][2024-06-25T22:59:03+1000][18069] parseFile():348 config.cc: ProtoJSON parse status: 'INVALID_ARGUMENT:Unexpected token.
tmp/bin", "isBind": tru } ] }
^'
[F][2024-06-25T22:59:03+1000][18069] parseArgs():577 Couldn't parse configuration from 'busybox-sh.json' file TextProto$ printf 'exec_bin { path: "/bin/sh" }\nmount { src: "/tmp/bin" dst: "/bin" is_bind: true }' > busybox-sh.cfg
$ ./nsjail -C busybox-sh.cfg
[I][2024-06-25T22:56:11+1000] Mode: STANDALONE_ONCE
[I][2024-06-25T22:56:11+1000] Jail parameters: hostname:'NSJAIL', chroot:'', process:'/bin/sh', bind:[::]:0, max_conns:0, max_conns_per_ip:0, time_limit:600, personality:0, daemonize:false, clone_newnet:true, clone_newuser:true, clone_newns:true, clone_newpid:true, clone_newipc:true, clone_newuts:true, clone_newcgroup:true, clone_newtime:false, keep_caps:false, disable_no_new_privs:false, max_cpus:0
[I][2024-06-25T22:56:11+1000] Mount: '/' flags:MS_RDONLY type:'tmpfs' options:'' dir:true
[I][2024-06-25T22:56:11+1000] Mount: '/tmp/bin' -> '/bin' flags:MS_RDONLY|MS_BIND|MS_REC|MS_PRIVATE type:'' options:'' dir:true
[I][2024-06-25T22:56:11+1000] Uid map: inside_uid:1000 outside_uid:1000 count:1 newuidmap:false
[I][2024-06-25T22:56:11+1000] Gid map: inside_gid:100 outside_gid:100 count:1 newgidmap:false
[I][2024-06-25T22:56:11+1000] Executing '/bin/sh' for '[STANDALONE MODE]'
/bin/sh: can't access tty; job control turned off
/ $ ✅ Nearly valid TextProto: $ sed -i s/true/tru/ busybox-sh.cfg
$ ./nsjail -C busybox-sh.cfg
[W][2024-06-25T22:58:34+1000][18055] parseFile():346 Config file 'busybox-sh.cfg' failed to parse as either TextProto or ProtoJSON
[W][2024-06-25T22:58:34+1000][18055] flushLog():316 config.cc: 'Error parsing text-format nsjail.NsJailConfig: 2:50: Invalid value for boolean field "is_bind". Value: "tru".'
[W][2024-06-25T22:58:34+1000][18055] parseFile():348 config.cc: ProtoJSON parse status: 'INVALID_ARGUMENT:Unexpected token.
exec_bin { path: "/b
^'
[F][2024-06-25T22:58:34+1000][18055] parseArgs():577 Couldn't parse configuration from 'busybox-sh.cfg' file
✅ File that is valid JSON and text protobufI can't think of such a test case! But forcing the if condition to true we see:
✅ |
a9e654d
to
acf9f45
Compare
@@ -302,39 +304,69 @@ static bool parseInternal(nsjconf_t* nsjconf, const nsjail::NsJailConfig& njc) { | |||
return true; | |||
} | |||
|
|||
static std::list<std::string> error_messages; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% on this. I see https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables advises against this, since list has a non-trivial destructor. Thoughts?
Great, thanks! |
We probably need also to remove the exception from file reading, so it behaves more controllably. I'll work on that ./nsjail --config '/sys/devices/pci0000:00/0000:00:03.1/0000:0a:00.0/0000:0b:00.0/0000:0c:00.2/usb5/5-2/5-2:1.0/0003:2516:014D.0001/input/input2/power/autosuspend_delay_ms'
terminate called after throwing an instance of 'std::__ios_failure'
what(): basic_filebuf::underflow error reading the file: Input/output error
Aborted (core dumped) |
JSON is an easier to generate than TextProto in some environments, e.g. Jsonnet or Nix.
https://protobuf.dev/reference/cpp/api-docs/google.protobuf.util.json_util/