Live data from Hacker News

The Quest to Secure chown and symlinks

buildkite.com

11–20 of 29 posts

Re: The Quest to Secure chown and symlinks

#11

I have a small project in this approximate area: https://www.kylheku.com/cgit/safepath/about/ safepath is a function which tries to analyze whether a path is safe to use. Roughly that means that it doesn't resolve in some way that can be controlled by another (non-root) user. A something similar to this is in TXR Lisp under the name path-components-safe: https://www.kylheku.com/cgit/txr/tree/stdlib/path-test.tl?hl...

This seems to still be vulnerable to TOCTOU?

Tho, to be fair, it's probably an improvement by narrowing how simple it is to exploit relative to doing nothing.

Re: The Quest to Secure chown and symlinks

#12
post #4

(cd "$path" && [ "$(pwd -P)" = "$path" ] && chown -R buildkite-agent:buildkite-agent .) the real question though is why they're trusting just Docker alone to isolate customers; if they want the jobs to effectively be a single user to the system, they can even use unprivileged user namespaces?

This stack is run by a single customer on trusted code isolated in their own AWS env. (I wrote it originally 6-7 years back)

There are radically better isolation strategies now. Firecracker and/or Sysbox hardened docker containers is one I’ve recently implemented.

Re: The Quest to Secure chown and symlinks

#13
post #2

test -L checks if a file is a symlink; no need for realpath comparisons (which is slower)

> checks if a file is a symlink

to expand on that:

"In computing, a symbolic link (also symlink or soft link) is a file whose purpose is to point to a file or directory (called the "target") by specifying a path thereto."

Re: The Quest to Secure chown and symlinks

#14
post #5

Why do the files have bad permissions to start with?

Docker is running as root, so the files written in mounted volumes get mapped to uid 0 on the host. When the agent then goes to re-use the checked out code, it can’t run ‘git clean’.

Username space remapping wasn’t adequate, for reasons I’m a bit blurry on. I think recent kernels have some better options on remapping permissions across file systems.

Re: The Quest to Secure chown and symlinks

#15

There would traditionally been another TOCTOU is the described solution, namely hardlinks. This can often be used to get root to do something to a file it shouldn't. The trad solution is to have user writeable areas (home, vartmp, tmp) on different volumes. Some tools have options to not traverse symlinks across volumes for this and other reasons. But on modern systems you are protected by the fs.protected_hardlinks…

Researching this, found [1]: > POSIX guarantees that hard links can exist. It follows that, on POSIX systems without any non-standard protections, it's unsafe for anyone (but in particular, root) to do anything sensitive in a directory that is writable by another user. Cross-platform programs designed to do so are simply flawed. I feel like something had to have been lost in translation from Unix to POSIX here. In th…

A link is just a directory entry. If you can write to the directory, you can create entries in it. And back in olden times, mkdir was setuid because it wasn't a syscall.

Re: The Quest to Secure chown and symlinks

#16
post #2

test -L checks if a file is a symlink; no need for realpath comparisons (which is slower)

Yeah but what do you do if your are not using shell?

Hint: it's stat(2) (or equivalent in your language library)

Additional hint: if using pythons os.stat set follow_symlinks to false. It recently took me an embarrassing long time to figure out why my script was failing to find symlinks.

Re: The Quest to Secure chown and symlinks

#17

There would traditionally been another TOCTOU is the described solution, namely hardlinks. This can often be used to get root to do something to a file it shouldn't. The trad solution is to have user writeable areas (home, vartmp, tmp) on different volumes. Some tools have options to not traverse symlinks across volumes for this and other reasons. But on modern systems you are protected by the fs.protected_hardlinks…

Researching this, found [1]: > POSIX guarantees that hard links can exist. It follows that, on POSIX systems without any non-standard protections, it's unsafe for anyone (but in particular, root) to do anything sensitive in a directory that is writable by another user. Cross-platform programs designed to do so are simply flawed. I feel like something had to have been lost in translation from Unix to POSIX here. In th…

Yes, normally you could hard link any file if you can read a directory that references it, and if there was a directory you had write access to on the same volume. The hard link doesn't change the permissions of the file.

Hard links are particularly useful for having multiple copies of a directory tree without consuming space for the files. Eg to create an overlay of a build tree, with your changes on top. Unfortunately CoW file systems still have many issues that make this difficult.

Re: The Quest to Secure chown and symlinks

#18
This just cements my conviction that file systems not having transactional operations is a huge omission nowadays. It really is time to start having file systems that are not just huge mutable spaces, and be more like proper ACID databases.

I hope somebody is working on it because as things are going in the last years, I'd be retired before I have the time for it.

Re: The Quest to Secure chown and symlinks

#19

This just cements my conviction that file systems not having transactional operations is a huge omission nowadays. It really is time to start having file systems that are not just huge mutable spaces, and be more like proper ACID databases. I hope somebody is working on it because as things are going in the last years, I'd be retired before I have the time for it.

How does that solve the problem of links pointing places where you don’t expect them to, or any of the other issues in this article?

The problem here is trying to cross a security boundary where your only tool is shell scripting. That’s just basically impossible to do securely.

Use a real programming language, follow the rules required to make it secure and do all the checks you need to.

Re: The Quest to Secure chown and symlinks

#20
post #4

(cd "$path" && [ "$(pwd -P)" = "$path" ] && chown -R buildkite-agent:buildkite-agent .) the real question though is why they're trusting just Docker alone to isolate customers; if they want the jobs to effectively be a single user to the system, they can even use unprivileged user namespaces?

When all you have is a hammer...
Post reply on HN