0

This question might sound specific but that is because I'm making my own ssh server (a company needs some specific thing) and it has SFTP but because the server is running as root, it's giving any user that uses the SFTP service access to everything.

For the bash sessions, it's not a problem because I'm running them like so

sudo -H -u $USER bash

but the SFTP I'm using doesn't have user authentication.

I was wondering how do SSH deals with this because I imagine that the default ssh server is also running as root but maybe the SFTP service that SSH uses allows for authentication.

I was thinking that every time a user logs in I can spawn another of my server but only for SFTP like this

sudo -u $USER bash -c "my_server -sftponly

and then the SFTP server will only have access to that users files but I think it's overkill.

Libraries

golang.org/x/crypto/ssh
github.com/pkg/sftp
1

I guess that tools relinquishing privileges from root to another user simply use the system calls provided for that purpose.

$ man setuid
SETUID(2)                 FreeBSD System Calls Manual                SETUID(2)

NAME
     setuid, seteuid, setgid, setegid - set user and group ID

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <unistd.h>

     int
     setuid(uid_t uid);

     int
     seteuid(uid_t euid);

     int
     setgid(gid_t gid);

     int
     setegid(gid_t egid);

DESCRIPTION
     The setuid() system call sets the real and effective user IDs and the
     saved set-user-ID of the current process to the specified value.  The
     setuid() system call is permitted if the specified ID is equal to the
     real user ID or the effective user ID of the process, or if the effective
     user ID is that of the super user.

     The setgid() system call sets the real and effective group IDs and the
     saved set-group-ID of the current process to the specified value.  The
     setgid() system call is permitted if the specified ID is equal to the
     real group ID or the effective group ID of the process, or if the
     effective user ID is that of the super user.

     The seteuid() system call (setegid()) sets the effective user ID (group
     ID) of the current process.  The effective user ID may be set to the
     value of the real user ID or the saved set-user-ID (see intro(2) and
     execve(2)); in this way, the effective user ID of a set-user-ID
     executable may be toggled by switching to the real user ID, then re-
     enabled by reverting to the set-user-ID value.  Similarly, the effective
     group ID may be set to the value of the real group ID or the saved set-
     group-ID.

RETURN VALUES
     Upon successful completion, the value 0 is returned; otherwise the
     value -1 is returned and the global variable errno is set to indicate the
     error.

ERRORS
     The system calls will fail if:

     [EPERM]            The user is not the super user and the ID specified is
                        not the real, effective ID, or saved ID.
5
  • Yes, but If the ssh server changes its privileges with SETUID to a specific user, then what happens when another user tries to connect? – Fransebas Mar 1 '20 at 18:39
  • Here is a diagram of what I'm doing i.imgur.com/EFreH6n.png – Fransebas Mar 1 '20 at 18:42
  • 1
    @Fransebas Servers typically fork() and then the child process changes privileges. – Jens Mar 1 '20 at 18:59
  • And do you think this is what ssh does? it might be the solution but I'm using go and fork is not "supported" you can still use it though – Fransebas Mar 1 '20 at 19:11
  • @Fransebas Yes, judging from OpenSSH's source code in uidswap.c, function temporarily_use_uid(), which uses seteuid(). – Jens Mar 1 '20 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.