3

I'm creating a PAM module for a project. The PAM module will be using a library that will be re-used by some command line utilities (rather than re-writing everything each time). In this library, I want to have it interpret policy that discriminate against and/or logs according to subnet memberships of the remote host. Near as I can tell this value is probably coming from the authenticating application, but I don't know. Since the shared object won't have access to the pamh structure from libpam I can't just do a pam_get_item (like I would be able to from the PAM module itself) so I've had to resort to other means.

The best solution I've come up with is to have the shared object look for a connected TTY, if it's there go to utmp and find the login process associated with that TTY, extract the IP address from there. If there isn't a TTY, assume it's an initial login of a network user. The library then iterates over the sockets (which I've defined as basically any symlink with the word "socket" in the target's filename when you do a ls -l /proc/<pid>/fd) and uses the socket inode number to cross reference with /proc/net/tcp and extracts the remote IP address associated with that inode number. If it doesn't find an inode there then it assumes it's Unix domain or tcp6 (IPv6 support in this is forthcoming and not terribly important for the near future). If it still isn't able to find it, assume that some daemon has called an application linking against it and interpret it as such (might do something eventually, if it's worthwhile, but for now it's just a big NOOP if the first two don't return anything.

It seems to work but I have some high level questions about how PAM is supposed to work:

  1. Is there some official standard that governs PAM operation? For example, is it covered by a POSIX standard somewhere? I know there are multiple PAM implementations (four or five that I've found thusfar) but I don't know if existing commonalities are de jure or de facto or just how I happen to have my system configured.

  2. After I did a ls -l /proc/<pid>/fd > /lsOutput from the module itself (via system()):

[root@hypervisor pam]# cat /lsOutput total 0

lrwx------. 1 root root 64 Jun 15 15:09 0 -> /dev/null

lrwx------. 1 root root 64 Jun 15 15:09 1 -> /dev/null

lrwx------. 1 root root 64 Jun 15 15:09 2 -> /dev/null

lr-x------. 1 root root 64 Jun 15 15:09 3 -> socket:[426180]

[root@hypervisor pam]#

And issuing a manual ls after the user logins in:

[root@hypervisor pam]# ls -l /proc/18261/fd
total 0
lrwx------. 1 root root 64 Jun 15 15:15 0 -> /dev/null
lrwx------. 1 root root 64 Jun 15 15:15 1 -> /dev/null
lrwx------. 1 root root 64 Jun 15 15:15 11 -> /dev/ptmx
lrwx------. 1 root root 64 Jun 15 15:15 12 -> /dev/ptmx
lrwx------. 1 root root 64 Jun 15 15:15 13 -> socket:[426780]
lrwx------. 1 root root 64 Jun 15 15:15 14 -> socket:[426829]
lrwx------. 1 root root 64 Jun 15 15:15 2 -> /dev/null
lrwx------. 1 root root 64 Jun 15 15:15 3 -> socket:[426180]
lrwx------. 1 root root 64 Jun 15 15:15 4 -> socket:[426322]
lr-x------. 1 root root 64 Jun 15 15:15 5 -> pipe:[426336]
l-wx------. 1 root root 64 Jun 15 15:15 6 -> pipe:[426336]
lrwx------. 1 root root 64 Jun 15 15:15 7 -> socket:[426348]
lrwx------. 1 root root 64 Jun 15 15:15 8 -> socket:[426349]
lrwx------. 1 root root 64 Jun 15 15:15 9 -> /dev/ptmx
[root@hypervisor pam]#

So basically, it seems like both the TTY and any additional sockets get opened only AFTER the session modules finish (my temporary test module's session handling is the last in the stack for the sshd service). I've been unable to get it to be otherwise (or even think of a time when the connecting client won't be a TCP socket at descriptor 3).

Is this just due to my lack of imagination or is it necessarily so? I'm leaning towards the latter as it would seem that communicating with the client would be a pre-requisite to doing pretty much anything else that's useful. I don't know that for sure, so I feel I should ask somebody. Will descriptor 3 always be the authenticating client (my .so only makes the assumption that it's the lowest numbered TCP socket, and only if there's no TTY, but it seems like 3 should always be the descriptor for the connecting client). Would pulling the first TCP descriptor be a "deterministic" way of establishing the remote client's identity? Or is there no prescribed way this is supposed to play out and that's just how either my system is configured or how SSH has chosen to interface with PAM?

  1. Is it sshd that's setting the rhost value or is that coming from some place else? I've tried grep-ing over the source code for both SSH and libpam, but no dice. I can see where libpam handles the setting of the host value when something call pam_set_item, but not were pam_set_item actually gets called to set it to be this or that particular host.

Any amount of help would be appreciated, I've googled but I'm starting to get splinters on my fingertips from scraping the bottom of the barrel.

Main reason I'm interested in knowing this is so that I'll end up not only with the "right" answer but mostly so that I won't have any surprises later on down the road. We have some Solaris platforms we may do this on, but my main motivation is to have assumptions that are grounded in things that are actually going to be constant.

I also realize that I could have the client programs/modules feed the host information to the library, but that would likely involve code re-write two or three times (as the CLI tools prepare session information from utmp and the PAM module from pam_get_item) and potentially make the project look more complex than it really needs to be.

1

1 Answer 1

0

Answering some of your questions:

"Is there some official standard that governs PAM operation?"

Apparently, yes. Wikipedia's entry on Pluggable_Authentication_Modulesays "PAM was standardized as part of the X/Open UNIX standardization process, resulting in the X/Open Single Sign-on (XSSO) standard." I have never found this particularly relevant to my dealings with it.

"Is this just due to my lack of imagination or is it necessarily so?"

<magicEightBall>"Concentrate and ask again"</magicEightBall> (It's ambiguous which "this" is being referred to - perhaps you can clarify?

"Will descriptor 3 always be the authenticating client?"

This is a behaviour of the application, rather than PAM.

Would pulling the first TCP descriptor be a "deterministic" way of establishing the remote client's identity?

Also a behaviour of the application.

"Is it sshd that's setting the rhost value or is that coming from some place else?"

It is sshd that sets the rhost value. In openssh's file auth-pam.c, function sshpam_init(), you'll find:

sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);

Some general notes:

  • rather than keying off the TTY - which, as you've discovered get set later - you can walk the process lineage via getppid() and /proc.
  • don't trust system() in PAM modules - it uses a user's environment, and may not be the one you expect. Use fork()/execlp() instead.
3
  • How does walking the process tree get me an IP address? I'm keying off TTY because that's what tells me that I haven't gone so far back as to possibly catch IP addresses associated with non-sessions (or worse, other sessions). Also, XSSO is an alternative PAM implementation (spec'd out by the Open Group) that works Kerberos/SSO into the mix (not an overarching standard). XSSO is along the lines of OpenPAM or Solaris's libpam in that way. For example, no other PAM implementation has XSSO's credentials caching, secondary signon, or username mapping.
    – Bratchley
    Jun 29, 2013 at 2:06
  • fwiw, I eventually found that there actually isn't an overarching standard for PAM (which is suprising to me) but some vague descriptions exist for the Linux-PAM implementation exist as part of LSB but it basically just gives a bunch of wishy-washy statements about what modules tend to do and what common conventions are. That's followed up by a basic description of the SPI and API Linux-PAM supports. It's possible that at some point they'll flesh it out and ensure different components offer more guarantees, but for now it's disturbingly vague.
    – Bratchley
    Jun 29, 2013 at 2:09
  • I've since decided to just bite the bullet and just do a setRemoteAddress()-like solution that sets an internal value that only gets looked at if the library can't find anything in utmp. I've decided there's no reliable way to determine remote addresses programatically. I wish this got set similar to how loginuid or something gets set (i.e by PAM-SPI, or even by core libpam would be better) but it isn't so you have to wait for the application to do utmp.
    – Bratchley
    Jun 29, 2013 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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