/usr/share/doc/pam-devel/html
NameSizeModeActions
adg-author.html30980644editdlrm
adg-copyright.html36240644editdlrm
adg-example.html36760644editdlrm
adg-files.html22390644editdlrm
adg-glossary.html32860644editdlrm
adg-interface-by-app-expected.html624720644editdlrm
adg-interface-of-app-expected.html84570644editdlrm
adg-interface-programming-notes.html26140644editdlrm
adg-interface.html50190644editdlrm
adg-introduction-description.html33730644editdlrm
adg-introduction-synopsis.html26830644editdlrm
adg-introduction.html20640644editdlrm
adg-libpam-functions.html134490644editdlrm
adg-libpam_misc.html34210644editdlrm
adg-overview.html83570644editdlrm
adg-porting.html43060644editdlrm
adg-security-conv-function.html23460644editdlrm
adg-security-library-calls.html32030644editdlrm
adg-security-resources.html29030644editdlrm
adg-security-service-name.html45350644editdlrm
adg-security-user-identity.html55120644editdlrm
adg-security.html38240644editdlrm
adg-see-also.html22140644editdlrm
Linux-PAM_ADG.html86130644editdlrm
Linux-PAM_MWG.html88030644editdlrm
mwg-author.html30750644editdlrm
mwg-copyright.html36030644editdlrm
mwg-example.html20050644editdlrm
mwg-expected-by-module-item.html464570644editdlrm
mwg-expected-by-module-other.html83520644editdlrm
mwg-expected-by-module.html41170644editdlrm
mwg-expected-of-module-acct.html61870644editdlrm
mwg-expected-of-module-auth.html109680644editdlrm
mwg-expected-of-module-chauthtok.html79770644editdlrm
mwg-expected-of-module-overview.html64370644editdlrm
mwg-expected-of-module-session.html71260644editdlrm
mwg-expected-of-module.html43740644editdlrm
mwg-introduction-description.html39710644editdlrm
mwg-introduction-synopsis.html20280644editdlrm
mwg-introduction.html20320644editdlrm
mwg-see-also.html22310644editdlrm
mwg-see-options.html29960644editdlrm
mwg-see-programming-libs.html30150644editdlrm
mwg-see-programming-sec.html91410644editdlrm
mwg-see-programming-syslog.html47410644editdlrm
mwg-see-programming.html30780644editdlrm
Edit: /usr/share/doc/pam-devel/html/mwg-see-programming-sec.html (9141B)
5.1. Security issues for module creation

5.1. Security issues for module creation

5.1.1. Sufficient resources

Care should be taken to ensure that the proper execution of a module is not compromised by a lack of system resources. If a module is unable to open sufficient files to perform its task, it should fail gracefully, or request additional resources. Specifically, the quantities manipulated by the setrlimit(2) family of commands should be taken into consideration.

5.1.2. Who´s who?

Generally, the module may wish to establish the identity of the user requesting a service. This may not be the same as the username returned by pam_get_user(). Indeed, that is only going to be the name of the user under whose identity the service will be given. This is not necessarily the user that requests the service.

In other words, user X runs a program that is setuid-Y, it grants the user to have the permissions of Z. A specific example of this sort of service request is the su program: user joe executes su to become the user jane. In this situation X=joe, Y=root and Z=jane. Clearly, it is important that the module does not confuse these different users and grant an inappropriate level of privilege.

The following is the convention to be adhered to when juggling user-identities.

  • X, the identity of the user invoking the service request. This is the user identifier; returned by the function getuid(2).

  • Y, the privileged identity of the application used to grant the requested service. This is the effective user identifier; returned by the function geteuid(2).

  • Z, the user under whose identity the service will be granted. This is the username returned by pam_get_user() and also stored in the Linux-PAM item, PAM_USER.

  • Linux-PAM has a place for an additional user identity that a module may care to make use of. This is the PAM_RUSER item. Generally, network sensitive modules/applications may wish to set/read this item to establish the identity of the user requesting a service from a remote location.

Note, if a module wishes to modify the identity of either the uid or euid of the running process, it should take care to restore the original values prior to returning control to the Linux-PAM library.

5.1.3. Using the conversation function

Prior to calling the conversation function, the module should reset the contents of the pointer that will return the applications response. This is a good idea since the application may fail to fill the pointer and the module should be in a position to notice!

The module should be prepared for a failure from the conversation. The generic error would be PAM_CONV_ERR, but anything other than PAM_SUCCESS should be treated as indicating failure.

5.1.4. Authentication tokens

To ensure that the authentication tokens are not left lying around the items, PAM_AUTHTOK and PAM_OLDAUTHTOK, are not available to the application: they are defined in <security/pam_modules.h>. This is ostensibly for security reasons, but a maliciously programmed application will always have access to all memory of the process, so it is only superficially enforced. As a general rule the module should overwrite authentication tokens as soon as they are no longer needed. Especially before free()'ing them. The Linux-PAM library is required to do this when either of these authentication token items are (re)set.

Not to dwell too little on this concern; should the module store the authentication tokens either as (automatic) function variables or using pam_[gs]et_data() the associated memory should be over-written explicitly before it is released. In the case of the latter storage mechanism, the associated cleanup() function should explicitly overwrite the *data before free()'ing it: for example,

/*
 * An example cleanup() function for releasing memory that was used to
 * store a password.
 */

int cleanup(pam_handle_t *pamh, void *data, int error_status)
{
    char *xx;

    if ((xx = data)) {
        while (*xx)
            *xx++ = '\0';
        free(data);
    }
    return PAM_SUCCESS;
}