9 ms·
Why pledge(2) or, how I learned to love web application sandboxing
- Sean1708 10y ago> allows resource requests or denies them and (hopefully) kills the application Why is killing the application desirable behaviour over just denying the request?
- pas 10y agoHelps debugging. Though a sysfs/procfs config for this would go a long way.
- shanemhansen 10y agoThere's a school of thought in security that believes you should hard fail at any attempt to violate an invariant. I believe the linux grsec patches do this. They will just crash the kernel if they see something at runtime that they don't like.
- aaronmdjones 10y agoIndeed; if you have the Active Kernel Exploit Response feature enabled in grsec, and a root process triggers a kernel OOPS, it will panic() the kernel. You can combine this with the panic= kernel command-line argument to set a reboot delay, but for headed (rather than headless) boxes it may be more desirable to keep the panic message up to see what process triggered it.
- forgottenpass 10y agoIf you detect something that can not (well, should not) ever happen, it means that state of the process is no longer within designed bounds. The process' output can not be trusted, so it should stop processing. Detecting something you never expect to happen is almost tautologically stating the software doesn't know how to recover state. This, of course, relies on an architecture where another process will take care of making sure an error is bubbled to the place it needs to go, pulling failsafe, etc... I will grant some environments and/or designs make this easier than others (eg. Erlang's error handling is all about letting processes die).
- dom0 10y agoBecause everyone in dev and ops ignores warnings. Only stuff that crashes is investigated.
- skrebbel 10y agoThe author writes from the perspective of writing a CGI-based web app. Here, every request becomes a new OS process, so killing the application is killing the request.
- Sanddancer 10y agoIf an application is making a request it hasn't marked as being one it is going to make, then chances are, there is a big problem that needs dealt with. In such a situation, crashing as soon as possible means that there's a better chance that all of the relevant data is there to pore through. If you just deny the call, then it could be many hundred of functions later, possibly with relevant data being freed up, and your job of picking through your program's entrails becomes all the more difficult.
- catern 10y agoSince the author is using cgi: >However, this is definitely something in the crosshairs of ksql(3): forking a process, like kcgi(3) does, that handles the database I/O and communicates with the master over pipes. If they do this, then they can just use seccomp SECCOMP_SET_MODE_STRICT, which is the first thing described in the seccomp man page and in the Wikipedia page. And is also the original/first mode of seccomp to be available. I believe capsicum would likewise be trivial to use: You can just immediately lock things down completely. (Perhaps just cap_enter() is sufficient?)
- loeg 10y agoIf a program just needs to communicate over open sockets, cap_enter() will restrict the program from opening more sockets. But the existing fds aren't restricted. You can use caph_limit_stream(fd, CAPH_READ/CAPH_WRITE) to restrict existing sockets down to only what is needed for stdio routines.
- justincormack 10y agoSECCOMP_SET_MODE_STRICT is pretty hard to use - you cannot allocate memory for example.
- catern 10y agoI did not realize that. Given that, I was incorrect when I said that the author (or anyone) could easily use seccomp MODE_STRICT. Capsicum may still be easy to use. And seccomp-bpf is not that complex either.
- qwertyuiop924 10y agopledge(2) is great. However, BCHS sucks. No. Do not write your webapp in C. I'm serious here. Write it in Python, write it in TCL, write it in Lisp. But write it an environment that is at least semi-manged. FFIs are pretty good. You can call pledge from your app. But don't write your app in C. If you're doing anything serious, it's a bad idea.
- catern 10y agoWell... there is at least some sense in writing a webapp in C. If you rely on operating system primitives for security (processes, Unix filesystem permissions, seccomp/pledge/capsicum) instead of language features (bounds checking, garbage collection, etc.) then just maybe you could get performance gains while still being secure. But in reality writing applications in a hosted environment/virtual machine is more performant than writing it in C and locking it down with operating system features. And of course more secure. The author's efforts would be better spent on improving the performance of OS-based security (processes, filesystems), than attempting to use that security as it stands. There are all kinds of tricks that could be implemented to improve things...
- aidenn0 10y agoIf you are using CGI, then you probably already don't care about performance. On top of that, the CPU throughput overhead of using a compiled managed language is typically less than a factor of 2, which, combined with the fact that the majority of web applications are not compute bound means the number of webapps that it would make any sense to write in C is nearly zero. Pretty much all pithy advice (such as "Don't write your webapps in C") has an implicit exception "Unless you fully understand why I made this recommendation and have good reasons for why they don't apply." That exception is not made explicit because 1) It would make the advice less pithy and 2) Then those who would most benefit from the advice convince themselves that it doesn't apply to them.
- SwellJoe 10y ago"If you are using CGI, then you probably already don't care about performance." So...I think you're missing a few pieces of the puzzle with this statement. The statement is a truism that is right almost always, but it has exceptions, and this is one of those exceptions. CGI is slow when startup time for your app is slow, because of a VM, because of the library loading overhead, because of environment setup, because of some thing that isn't really the fault of CGI per se. CGI can be fast, or at least fast enough, if startup time for your app is very fast. An app written in C, statically linked, could potentially start and respond faster than many dynamic language apps could even when running in an app server. C is fast. C is small. C is also particularly error-prone, would be a little verbose for web apps, and does not have a good ecosystem of support libraries for web apps, but even if running it without an app server in a CGI mode it could be fast enough for most deployments. The CGI interface is not why CGI apps are slow. CGI apps are slow because startup of many modern language environments is slow. C is a language that doesn't have to be slow in that context. (Though, it could be faster still by using an FCGI or other app server model and having your C web app running all the time, so it just has to answer requests over a socket or whatever...though async programming in C is also hard to get right for inexperienced programmers.)
- loeg 10y agoThe author dismisses capsicum without much thought. Really, any application you've already restructured to be sandboxed under pledge(2) is trivial to capsicumize.
- CJefferson 10y agoDo you know of any good guides for doing that? I tried capsicum at some point but got lost.
- tachion 10y agoYou may learn a lot from FreeBSD reviews about sandboxing, like these: https://reviews.freebsd.org/D8543 https://reviews.freebsd.org/D8543 https://reviews.freebsd.org/D8290 https://reviews.freebsd.org/D8290
- emaste 10y agoAlso have a look at https://wiki.freebsd.org/Capsicum https://wiki.freebsd.org/Capsicum, which has links to many examples of already-Capsicumized applications and work in progress.
- feld 10y agoAnd if you want to write your web app in Python, you can still use capsicum http://www.freshports.org/devel/py-pycapsicum/ http://www.freshports.org/devel/py-pycapsicum/ edit: people, if you have the skills please write more bindings for more languages
- aomix 10y agoThat is probably the reason for the quick uptake of pledge in OpenBSD's tree. A lot of their software had already been restructured or rewritten for privilege separation. But looking at a few capsicum diffs in the FreeBSD codebase I'm not sure I'd call those trivial. You can do more with capscium so there is more code to deal with those abilities. But even simple cases hit 20+ lines of code with multiple failure states to deal with. Compared to the usual two line pledge diffs that's quite a bit more work to do.
- loosescrews 10y agoGo (golang) is a great language for web applications and has comparatively small syscall coverage making it easy to sandbox with seccomp(2). It also has most of the protections of a fully managed language, makes making raw syscalls easy, and can be used without libc. If you don't use cgo or SWIG (pure Go and Go assembly are fine), building statically linked pure Go binaries without libc is just a couple of commandline flags. If you don't import the net package or a few other problematic packages, no commandline flags are required.
- the8472 10y agotoo bad you can't do it from within the application itself. some of the security-related system calls (e.g. unshare) expect the application to be single-threaded or at least require acute awareness of threading. go's automatic threading makes this difficult.
- Someone 10y agoApart from the 'easy to sandbox' (also debatable, as that depends way more on the functionality of the application than on what it is written in, but let's ignore that) does that matter much? pledge isn't only, or even primarily, a defense against you accidentally making syscalls you don't want to make, but a defense against malware that, having invaded your application's process, attempts to make such syscalls pretending to be you. That's code you didn't write, making the programming language choice moot.
- loosescrews 10y agoNot really. An effective sandbox is restrictive. Managed languages tend to use a more limited set of system calls than non-managed languages because most libraries don't make any system calls of their own. The Go runtime and standard library use a more limited set of system calls than most alternatives, so a more limited set of sandbox filters are required. Injected instructions are more likely to invoke their own system calls, so you are more likely to catch it if you have a more restrictive set of filters.
- 10y ago
- egh5oon 10y agoFirejail is one of the most underrated: https://firejail.wordpress.com/ https://firejail.wordpress.com/
- deleted 10y ago[deleted]
- mioelnir 10y agoDoes pledge(2) still enable its simple programming interface by allowing certain default actions based on a filesystem hierarchy layout, statically defined in the kernel/libc/somewhere? The first presented versions of tame(2) had this iirc.
- wahern 10y agoIf I followed the tech@ list properly, I believe OpenBSD has added a specialized socket type for the "dns" pledge. That allows removing the code that permit certain socket calls iif /etc/resolv.conf has been opened. There are other things in the works related to pledge. For example, they're going to ship a native local stub resolver that will be a functional extension of libc, permitting them to remove and simplify code from libc. That would allow sandboxing networked services without necessarily requiring any filesystem access after invoking pledge, while still permitting DNS to seamlessly work even after configuration changes. (The trick is that most client resolvers, third-party and in libc, default to 127.0.0.1:53 if /etc/resolv.conf is unavailable.)
- comex 10y agoThe difference between pledge and seccomp is not just a matter of fine-grainedness. There's some of that, but I'd say there's also a difference in philosophy: sandboxing resources versus sandboxing kernel attack surfaces. At one end of the spectrum is the macOS sandbox, which pretty much only restricts how the process is allowed to interact with the outside world. It's not actually simplistic like the article claims; that's just the public API. If you look at the raw sandbox profiles, which Apple writes for daemons and such, it's quite fine-grained: sandbox profiles can specify which paths can be read and which can be written (with an arbitrary number of allow/deny rules, each of which can match on an arbitrary regular expression), which network ports can be opened or listened to, which preferences can be read or written, which IPC services can be accessed, and so on... there are a ton of categories. If you're on macOS, look in /usr/share/sandbox and /System/Library/Sandbox/Profiles to see what they look like. However, sandboxing only occurs when a given syscall's kernel implementation explicitly calls for it, and even the most locked-down process can access hundreds of syscalls and Mach kernel IPC calls, which based on their intended purpose should be harmless. And most of them really are harmless, but all it takes is a small oversight in one of them for an attacker to potentially corrupt memory in the kernel and gain control over the system. * At the other end is Linux seccomp, which is designed to allow minimizing kernel attack surface. Chrome's renderer processes only get access to a tiny set of syscalls, and even the arguments to the syscalls are locked down, corresponding as closely as possible to what the process actually uses. (This is possible in part because Chrome has an unsandboxed master process that handles things like reading files for it. The renderer sandbox doesn't even allow open(2).) Thus Chrome is protected from most vulnerabilities in the Linux kernel, because there's just no way to get to the buggy code in question from within the sandbox. This isn't perfect: I've found multiple Linux kernel bugs (only one exploitable so far) that are accessible from the sandbox. And of course it doesn't make Chrome immune to privilege escalation, since attacking the kernel directly isn't the only option; IPC communication with the master process is a much larger attack surface. But that's basically unavoidable, and the situation for Chrome is still a big improvement over other operating systems. Unfortunately, as the article says, it takes quite a lot of work to adapt to new applications. OpenBSD pledge seems somewhere in between. Unlike the other two, I don't have personal experience using it, but based on the manpage - an empty "promises" set restricts the process to _exit(2), which like seccomp should protect against kernel flaws. However, if you supply "stdio", suddenly the process gets access to 69 system calls, which is not terrible (many of the calls in the set are very basic and unlikely to contain bugs) but not as minimal as one might want. There are a bunch of random functionality lockdowns, which is good - for example, ioctl operations are locked down, "setsockopt(2) has been reduced in functionality substantially"... on the other hand, the promise categories generally seem more aligned to 'resources the program might want to access' than 'kernel API surface the program might need to use'. Overall, definitely not bad, and I'd like to see something comparable on Linux, but I'd be more comfortable if OpenBSD also supported a more fine-grained sandbox. * (Okay, that's a little unfair to macOS, since there are some sandbox filters that lock down kernel attack surface, such as the ioctl filter and the IOKit filters - the latter of which has been tightened over time. But that still leaves a ton of surface exposed.)
- marktangotango 10y agoI'm confused about what exactly 'sandboxing' entails here. Seems to be limiting syscalls and file system interactions. But what about process heap and cpu usage? Are these handled by either seccomp or capsicum?
- loeg 10y agoNo, capsicum is a security sandbox. Resource usage is not governed by capsicum. For that you can use traditional ulimits or RCTL for finer-grained control (similar to Linux cgroups).
- acidx 10y agoShameless plug, inspired by pledge(2), if you're a Linux rather than BSD person: how to use elementary virus writing techniques to sandbox programs under Linux using seccomp-bpf: https://tia.mat.br/posts/2016/11/08/infect_to_protect.html https://tia.mat.br/posts/2016/11/08/infect_to_protect.html