Skip to contentSkip to conversation

netlink nlmsg_pid supposed to be pid or tid?

DISCUSSION Applied Christopher Friesen 4 participants 14 messages Nov 21, 2005 – Nov 23, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

When using netlink, should "nlmsg_pid" be set to pid (current->tgid) or tid (current->pid)?

Chris

#1 lore raw
Quoted 2 lines · Friesen, Christopher [CAR:VC21:EXCH] wrote:

When using netlink, should "nlmsg_pid" be set to pid (current->tgid) or tid (current->pid)?

The reason I ask is that with 2.6.10 we had to set it to tid, which really doesn't make much sense given than sockets are shared across all threads in a process (and gettid() isn't even a libc function).

Chris

#2 lore raw
In reply to Christopher Friesen
Quoted 3 lines · Christopher Friesen

When using netlink, should "nlmsg_pid" be set to pid (current->tgid) or tid (current->pid)?

I tend to agree with you here that tgid makes more sense. Dave, what do you think?

However, you should never assume whatever value the kernel uses since it may always choose an arbitrary value should the preferred pid/tgid be taken by someone else.

So always use getsockname(2) to find out the correct address.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw

Hello!

Quoted 1 line

I tend to agree with you here that tgid makes more sense.

I agree, apparently netlink_autobind was missed when sed'ing pid->tgid. Of course, it does not matter, but tgid is nicer choice from user's viewpoint.

Alexey

#4 lore raw
Quoted 8 lines · Alexey Kuznetsov

Hello!

Quoted 1 line

I tend to agree with you here that tgid makes more sense.

I agree, apparently netlink_autobind was missed when sed'ing pid->tgid. Of course, it does not matter, but tgid is nicer choice from user's viewpoint.

I'm glad you agree, but I'm not sure what you mean by "it does not matter".

TIPC wants the user to fill in the pid to use in the nlmsghdr portion of a particular message.

When an NPTL child thread uses getpid() to specify the pid, it never receives a response to this request. Running the same code on the parent works, and running the same code under Linuxthreads works.

Using gettid() works, but it also means that only the thread that issued the request can read the reply.

Chris

#5 lore raw
Quoted 4 lines · Christopher Friesen

When an NPTL child thread uses getpid() to specify the pid, it never receives a response to this request. Running the same code on the parent works, and running the same code under Linuxthreads works.

As I said before, you should not rely on getpid() to work. Any other process in the system can bind to your pid which makes it unavailable to your process. In which case the kernel will pick an arbitrary negative pid as your address.

You should always use getsockaddr(2) to get your local address.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#6 lore raw
Quoted 3 lines · Herbert Xu

As I said before, you should not rely on getpid() to work.

You should always use getsockaddr(2) to get your local address.

Should that be getsockname(2)? Anyways, I now understand the issues.

As it turns out, we were actually already calling getsockname() a bit earlier in the code path, so we can just use the "pid" value from there.

Chris

#7 lore raw
Quoted 2 lines · Christopher Friesen

Should that be getsockname(2)? Anyways, I now understand the issues.

Yes that's what I meant.

Quoted 2 lines

As it turns out, we were actually already calling getsockname() a bit earlier in the code path, so we can just use the "pid" value from there.

Excellent.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#8 lore raw
In reply to Christopher Friesen

Hello!

Quoted 2 lines

TIPC wants the user to fill in the pid to use in the nlmsghdr portion of a particular message.

It is wrong. netlink_pid used not to be associated with process pids. Kernel used pid just as a seed to calculate a random value to bind, when user did not bind explicitly. It is equal to current->pid occasionally. F.e. libnetlink from iproute autobinds and gets netlink_pid with getsockname().

When user binds the socket himself, he was free to bind to any value, including pid and tgid.

Actually, I remember one discussion. Herbert, wait a minute... That's it: February 2005, Subject: [PATCH] Add audit uid to netlink credentials We decided (or not?) that binding to anything but tgid and pid must be prohibited by security reasons. Apaprently, the finding was lost.

Alexey

#9 lore raw
Quoted 5 lines · Alexey Kuznetsov

Actually, I remember one discussion. Herbert, wait a minute... That's it: February 2005, Subject: [PATCH] Add audit uid to netlink credentials We decided (or not?) that binding to anything but tgid and pid must be prohibited by security reasons. Apaprently, the finding was lost.

Thanks for reminding me. We may still need to track that down (we have now serialised most of the netlink processing so this my not be as bad as it was).

However, I think explicit binding should still be allowed for root, so nobody should take the PID for granted.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#10 lore raw

[NETLINK]: Use tgid instead of pid for nlmsg_pid

In reply to Alexey Kuznetsov
Quoted 3 lines · Alexey Kuznetsov

I agree, apparently netlink_autobind was missed when sed'ing pid->tgid. Of course, it does not matter, but tgid is nicer choice from user's viewpoint.

Great, here is the patch to do just that.

Thanks,

Signature · 5 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
net/netlink/af_netlink.c +1 -1
@@ -476,7 +476,7 @@ static int netlink_autobind(struct socke
 	struct hlist_head *head;
 	struct sock *osk;
 	struct hlist_node *node;
-	s32 pid = current->pid;
+	s32 pid = current->tgid;
 	int err;
 	static s32 rover = -4097;
 
maintainer Applied
#11 lore raw

Re: [NETLINK]: Use tgid instead of pid for nlmsg_pid

From: Herbert Xu <[email protected]>
Date: Tue, 22 Nov 2005 09:16:27 +1100

Quoted 8 lines
Quoted 3 lines · Alexey Kuznetsov

I agree, apparently netlink_autobind was missed when sed'ing pid->tgid. Of course, it does not matter, but tgid is nicer choice from user's viewpoint.

Great, here is the patch to do just that.

Applied, of course.

I can't for the life of me figure out how we missed this when we fixed up all the current->pid references under net/. Ulrich Drepper let us know that the problem existed, and I was sure we eliminated all such cases.

It is possible we accidently reintroduced current->pid when we redid all of the netlink hashing. :-)

#12 lore raw

Re: [NETLINK]: Use tgid instead of pid for nlmsg_pid

Quoted 2 lines · David S. Miller

Applied, of course.

Thanks Dave.

Quoted 2 lines

It is possible we accidently reintroduced current->pid when we redid all of the netlink hashing. :-)

I just checked using git-whatchanged and that line goes back to 2002 :)

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#13 lore raw

Re: [NETLINK]: Use tgid instead of pid for nlmsg_pid

From: Herbert Xu <[email protected]>
Date: Wed, 23 Nov 2005 11:03:37 +1100

Quoted 6 lines · David S. Miller
Quoted 2 lines

It is possible we accidently reintroduced current->pid when we redid all of the netlink hashing. :-)

I just checked using git-whatchanged and that line goes back to 2002 :)

Ho hum, I guess we just missed it on the current->pid scan then :-)