Skip to content
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

x/net/unix: support credentials on unix sockets #1101

Closed
rminnich opened this issue Sep 13, 2010 · 39 comments
Closed

x/net/unix: support credentials on unix sockets #1101

rminnich opened this issue Sep 13, 2010 · 39 comments

Comments

@rminnich
Copy link
Contributor

It would be useful to have these three functions:
func (c *UnixConn) Pid (pid int, err os.Error)

func (c *UnixConn) Uid (uid int, err os.Error)

func (c *UnixConn) Gid (gid int, err os.Error)

or even this:
func (c *UnixConn) PeerCred (pid, uid, gid int, err os.Error)

These are returned by SO_PEERCRED. This info is used just often enough
that these functions would come in
very handy.

I would submit a patch but my lawyers are still working on the CLA.

Thanks

ron
@rminnich
Copy link
Contributor Author

Comment 1:

Sorry about that useless issue subject, hit the button too soon :-(

@rsc
Copy link
Contributor

rsc commented Sep 14, 2010

Comment 2:

Status changed to Accepted.

@alberts
Copy link
Contributor

alberts commented Sep 16, 2010

Comment 3:

We are also interested in a related issue, namely sending file descriptors using       
SCM_RIGHTS.

@alberts
Copy link
Contributor

alberts commented Oct 5, 2010

Comment 4:

The best interface I could come up with is:
func (c *UnixConn) SendRights(fds []os.File) os.Error
func (c *UnixConn) SendCredentials(cred Ucred) os.Error
func (c *UnixConn) ReceiveAncillary() (fds []os.File, cred []Ucred, err os.Error)
As far as I understand the documentation, recvmsg can receive a mixed bag of cmsgs, so
there's no way of separating them beforehand.
Comments?

@bradfitz
Copy link
Contributor

bradfitz commented Oct 5, 2010

Comment 5:

rather than sending/receiving os.File, which is:
type File struct {
    fd      int
    name    string
    dirinfo *dirInfo // nil unless directory being read
    nepipe  int      // number of consecutive EPIPE in Write
}
... why not just send the fd int?  We wouldn't have a name, dirinfo, or nepipe, anyway?
If they want an os.File they can make one with os.Newle(fd int, name string) *os.File

@alberts
Copy link
Contributor

alberts commented Oct 5, 2010

Comment 6:

As discussed here:
http://groups.google.com/group/golang-nuts/browse_thread/thread/73a5186029a8d670
it seems most of sockets, pipes, etc. will be getting a Fd() that returns *os.File, so
based on that I reasoned that defining the API in terms of os.File made more sense.
As you pointed out, ReceiveAncillary will have to use os.NewFile then...

@alberts
Copy link
Contributor

alberts commented Oct 7, 2010

Comment 7:

More discussion:
http://groups.google.com/group/golang-dev/t/464c20c2e8363a92
Having a ReceiveAncillary on its own isn't going to work.

@alberts
Copy link
Contributor

alberts commented Oct 27, 2010

Comment 8:

Something to look out for: The kernel doesn't translate PIDs in SCM_CREDENTIALS across
PID namespaces (bug in 2.6.30).
Google Breakpad has some code related to this:
http://google-breakpad.googlecode.com/svn-history/r515/trunk/src/client/linux/crash_generation/crash_generation_server.cc

@alberts
Copy link
Contributor

alberts commented Nov 1, 2010

Comment 9:

Work in progress is here:
http://golang.org/cl/2331044/
Hopefully this will get merged soon, after which we can tackle the high-level API.

@davecheney
Copy link
Contributor

Comment 10:

Hi Fullung, 
2331044 was merged some time ago, what are the next steps from here ?

@alberts
Copy link
Contributor

alberts commented Mar 13, 2011

Comment 11:

We have the functionality to send and receive bytes using sendmsg/recvmsg, but we need
to create some functions to go from the specific ancillary messages to []byte and back.
For SCM_CREDENTIALS, we need to convert syscall.Ucred to bytes. We also need an API to
enable SO_PASSCRED on the socket.
SCM_RIGHTS is more tricky. In C there's a bunch of CMSG_* macros for packing in the file
descriptors you want to send. See man 3 cmsg. Up until now I've just wrapped these
macros with cgo, but that's probably not ideal.
I've written some test code to this stuff. I'll upload it on Monday.

@alberts
Copy link
Contributor

alberts commented Mar 14, 2011

Comment 12:

Here's some test code I used at one point:
http://codereview.appspot.com/download/issue2331044_53001_54003.diff
Also, there was another CL to fix Recvmsg:
http://golang.org/cl/3766042
I've also attached some code for dealing with cmsg. But we probably want to do something
that doesn't depend on cgo.

Attachments:

  1. cmsg.h (367 bytes)
  2. net_test.go (1424 bytes)
  3. cmsg.go (1654 bytes)

@alberts
Copy link
Contributor

alberts commented Mar 23, 2011

Comment 13:

http://www.normalesup.org/~george/comp/libancillary/
might provide some inspiration.

@rsc
Copy link
Contributor

rsc commented Dec 9, 2011

Comment 14:

Labels changed: added priority-later.

@alberts
Copy link
Contributor

alberts commented Mar 17, 2012

Comment 15:

syscall/sockcmsg_linux.go and syscall/sockcmsg_unix.go has more stuff in this area these
days. Maybe it's good enough?

@bradfitz
Copy link
Contributor

Comment 16:

It seems like this all works.  Here's a new test for passing fds:
http://golang.org/cl/5849057/
Should we close this?  Is anything missing from this bug?

@alberts
Copy link
Contributor

alberts commented Mar 17, 2012

Comment 17:

I wrote a SO_PASSCRED test, but it digs around in sysfd because of the lack of an API
for doing that. Might still be worth it to add it:
http://codereview.appspot.com/download/issue2331044_53001_54003.diff
I've also attached a test I have in our local tree for testing the basic stuff in
syscall. I think I wrote it before it was easy to test stuff in the syscall package.
It might be worth it to include these.

Attachments:

  1. sockcmsg_unix_test.go (959 bytes)

@bradfitz
Copy link
Contributor

Comment 18:

fullung, I just submitted my test, but feel free to send out more tests to golang-dev.

@alberts
Copy link
Contributor

alberts commented Mar 18, 2012

Comment 19:

Okay, will do later today.

@alberts
Copy link
Contributor

alberts commented Mar 18, 2012

Comment 20:

I've submitted my test for SCM_CREDENTIALS and SO_PASSCRED on Linux:
http://golang.org/cl/5846059
What's missing to completely close out this case:
Support for parsing SCM_CREDS messages in syscall for the BSDs and Darwin.
A way to enable SO_PASSCRED or the Darwin/BSD equivalent on a UnixConn with setsockopt
without breaking timeouts. This is related to issue #2458 in that you can't just call
File() on the socket to get its descriptor to pass to syscall.SetsockoptInt or whatever.

@alberts
Copy link
Contributor

alberts commented Mar 18, 2012

Comment 21:

What's also potentially missing is some plan for SO_PEERCRED. SO_PASSCRED allows
credentials to be sent, and root can send anything. SO_PEERCRED allows a local
credentials query of the remote end by the local end.
http://welz.org.za/notes/on-peer-cred.html

@alberts
Copy link
Contributor

alberts commented Mar 18, 2012

Comment 22:

func (c *UnixConn) PeerCred (pid, uid, gid int, err os.Error)
is probably easy to add after Go 1. It's just a getsockopt call.
We could also add
func (c *UnixConn) SetPassCred(enabled bool) error
after Go 1. Just a setsockopt.
All that's missing then is SCM_CREDS for Darwin/BSDs in syscall.
Thoughts?

@rminnich
Copy link
Contributor Author

Comment 23:

Sorry I did not reply but I've been on vacation, I think I'm one of
the guys who brought this up.
At present I'm holding off waiting for Go 1 before I do any more Go :-)
I'll take a look at that point.

@alberts
Copy link
Contributor

alberts commented Mar 19, 2012

@alberts
Copy link
Contributor

alberts commented Apr 1, 2012

Comment 25:

Now that Go 1 is out we should probably start the discussion about where these tests are
supposed to live.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 26:

Labels changed: added go1.1maybe.

@alberts
Copy link
Contributor

alberts commented Feb 4, 2013

Comment 27:

For run around SO_PASSCRED to think about:
Autobind Feature
       
If a bind(2) call specifies addrlen as sizeof(sa_family_t), or the SO_PASSCRED socket
option was specified for a socket that was not explicitly bound to an address, then the
socket is autobound to an abstract address. The address consists of a null byte followed
by 5 bytes in the character set [0-9a-f]. (Thus, there is a limit of 2^20 autobind
addresses.)

@alberts
Copy link
Contributor

alberts commented Feb 4, 2013

Comment 28:

For fun around SO_PASSCRED to think about:
Autobind Feature
       
If a bind(2) call specifies addrlen as sizeof(sa_family_t), or the SO_PASSCRED socket
option was specified for a socket that was not explicitly bound to an address, then the
socket is autobound to an abstract address. The address consists of a null byte followed
by 5 bytes in the character set [0-9a-f]. (Thus, there is a limit of 2^20 autobind
addresses.)

@rsc
Copy link
Contributor

rsc commented Mar 12, 2013

Comment 29:

[The time for maybe has passed.]

Labels changed: removed go1.1maybe.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 30:

I'm not sure I want to pollute the API so much, but I suppose UnixConn already exists.

Labels changed: added go1.2maybe.

Status changed to Thinking.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 31:

Labels changed: added feature.

@robpike
Copy link
Contributor

robpike commented Aug 16, 2013

Comment 32:

Will not happen for 1.2.

Labels changed: added go1.3maybe, removed go1.2maybe.

@robpike
Copy link
Contributor

robpike commented Aug 20, 2013

Comment 33:

Labels changed: removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 34:

Labels changed: added go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 35:

Labels changed: removed feature.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 36:

Labels changed: added release-none, removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 37:

Labels changed: added repo-main.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@mikioh mikioh changed the title net: support credentials on unix sockets x/net/unix: support credentials on unix sockets Sep 25, 2016
@jeffallen
Copy link
Contributor

Passing fd's using SCM_RIGHTS is already supported: https://golang.org/src/syscall/syscall_unix_test.go?h=TestUnixRightsRoundtrip#L260

Passing credentials is already supported: https://golang.org/src/syscall/creds_test.go?h=TestSCMCredentials#L21

The autobind stuff is not supported but if someone needs it, they can implement it themselves with the pieces that already exist in the stdlib.

Please close this issue.

@ianlancetaylor
Copy link
Contributor

@jeffallen Thanks. Closing.

@golang golang locked and limited conversation to collaborators Oct 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants