You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am running an SFTP server as root, but I need to restrict users accessing the SFTP server to operate under a specific UID and GID.
Since unix.Setfsuid() and unix.Setfsgid() can be set at the thread level, I implemented this in my fork of the sftp package. To ensure each SFTP worker operates with the correct UID/GID, I modified the runWorker function in Serve() to:
Use runtime.LockOSThread() to bind the worker to a specific thread.
Set fsuid and fsgid at the beginning of each worker routine.
Unlock the thread when the worker exits.
I introduced a new option WithFileSystemUidGid(uid, gid), which allows users to define a specific UID and GID for file system access.
Is this the recommended approach for implementing per-worker UID/GID restriction in the sftp package? If there are better ways to achieve this functionality, I’d appreciate any guidance.
Thanks,
Bruckins
The text was updated successfully, but these errors were encountered:
The recommended approach for implementing UID/GID restrictions is to do the same thing that the openssh SFTP server does: that is, have sshd handle inbound requests, do the necessary process of authenticating and authorizing the credentials, then fork+execing into the sftp-server binary. Generally, the only safe way to handle per-user requests is to run a process per user. Otherwise, there will always be that root access sitting available on another goroutine, just waiting for a hacker to find a way to exploit it.
There are numerous security concerns when trying to manage the SSH credentials and assumption of user id. These concerns may seem trivial at the time, but it turns out that very minor things can be leveraged against you. Humans are just not used to the idea that if you don’t screw down the bolt on your Ikea bookcase to the precise torque specification, that your house becomes vulnerable to break ins. Remember: ⟨tapping the sign⟩ do not roll your own crypto (or security).
Now, to the meat of your question: would I recommend this LockOSThread and Setfsuid. No. Golang uses pooled OS threads to run goroutines by design. Locking OS threads on a relatively permanent basis violates a core assumption of how Go works, and could have unexpected consequences on performance. Plus, if that locked OS thread spins off its own goroutines, now that goroutine has escaped your jail, and has free reign and free access. Having a security system that can be defeated by the difference of go doFunction() instead of doFunction() is generally not a good idea.
Hi,
I am running an SFTP server as root, but I need to restrict users accessing the SFTP server to operate under a specific UID and GID.
Since
unix.Setfsuid()
andunix.Setfsgid()
can be set at the thread level, I implemented this in my fork of the sftp package. To ensure each SFTP worker operates with the correct UID/GID, I modified therunWorker
function inServe()
to:runtime.LockOSThread()
to bind the worker to a specific thread.fsuid
andfsgid
at the beginning of each worker routine.I introduced a new option
WithFileSystemUidGid(uid, gid)
, which allows users to define a specific UID and GID for file system access.Code Changes (Inside
Serve()
inrunWorker
):Is this the recommended approach for implementing per-worker UID/GID restriction in the sftp package? If there are better ways to achieve this functionality, I’d appreciate any guidance.
Thanks,
Bruckins
The text was updated successfully, but these errors were encountered: