man-pages/man2/request_key.2

512 lines
14 KiB
Groff
Raw Normal View History

.\" Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
.\" Written by David Howells (dhowells@redhat.com)
.\" and Copyright (C) 2016 Michael Kerrisk <mtk.man-pages@gmail.com>
.\"
.\" %%%LICENSE_START(GPLv2+_SW_ONEPARA)
.\" This program is free software; you can redistribute it and/or
.\" modify it under the terms of the GNU General Public License
.\" as published by the Free Software Foundation; either version
.\" 2 of the License, or (at your option) any later version.
.\" %%%LICENSE_END
.\"
memusage.1, memusagestat.1, pldd.1, accept.2, adjtimex.2, arch_prctl.2, bdflush.2, bpf.2, close.2, epoll_ctl.2, epoll_wait.2, execve.2, execveat.2, fanotify_init.2, fanotify_mark.2, fcntl.2, fsync.2, get_kernel_syms.2, getdomainname.2, getgroups.2, gethostname.2, getrandom.2, getrlimit.2, getrusage.2, getsid.2, getunwind.2, io_getevents.2, ioctl_fat.2, kexec_load.2, killpg.2, listxattr.2, lseek.2, madvise.2, memfd_create.2, mknod.2, mlock.2, modify_ldt.2, msgctl.2, msgget.2, msgop.2, readlink.2, readv.2, reboot.2, recvmmsg.2, rename.2, request_key.2, restart_syscall.2, sched_setaffinity.2, sched_setattr.2, sched_setparam.2, seccomp.2, select_tut.2, semctl.2, semget.2, semop.2, set_thread_area.2, seteuid.2, setgid.2, setpgid.2, setresuid.2, setreuid.2, setsid.2, setuid.2, shmctl.2, shmget.2, shmop.2, sigaction.2, sigprocmask.2, stat.2, symlink.2, syscall.2, sysctl.2, unlink.2, bindresvport.3, byteorder.3, dlopen.3, endian.3, error.3, ffs.3, fmemopen.3, getcwd.3, getlogin.3, getnetent.3, getprotoent.3, getservent.3, getumask.3, getutent.3, glob.3, isalpha.3, lio_listio.3, login.3, mbsinit.3, mbstowcs.3, mbtowc.3, mkstemp.3, nextup.3, ntp_gettime.3, posix_fallocate.3, posix_spawn.3, pthread_join.3, pthread_rwlockattr_setkind_np.3, random.3, rcmd.3, realpath.3, resolver.3, setjmp.3, setnetgrent.3, sigvec.3, strerror.3, strverscmp.3, system.3, toupper.3, towlower.3, towupper.3, wcstombs.3, wordexp.3, cciss.4, loop.4, mouse.4, random.4, core.5, group.5, hosts.5, resolv.conf.5, ascii.7, environ.7, epoll.7, glob.7, ip.7, mq_overview.7, packet.7, pipe.7, raw.7, sched.7, signal.7, socket.7, symlink.7, ld.so.8, sln.8: tstamp Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2016-10-08 10:53:47 +00:00
.TH REQUEST_KEY 2 2016-10-08 Linux "Linux Key Management Calls"
.SH NAME
request_key \- request a key from the kernel's key management facility
.SH SYNOPSIS
.nf
.B #include <sys/types.h>
.B #include <keyutils.h>
.sp
.BI "key_serial_t request_key(const char *" type ", const char *" description ,
.BI " const char *" callout_info ,
.BI " key_serial_t " dest_keyring ");"
.fi
No glibc wrapper is provided for this system call; see NOTES.
.SH DESCRIPTION
.BR request_key ()
attempts to find a key of the given
.I type
with a description (name) that matches the specified
.IR description .
If such a key could not be found, then the key is optionally created.
If the key is found or created,
.BR request_key ()
attaches it to the keyring whose ID is specified in
.I dest_keyring
and returns the key's serial number.
.BR request_key ()
first recursively searches for a matching key in all of the keyrings
attached to the calling process.
The keyrings are searched in the order: thread-specific keyring,
process-specific keyring, and then session keyring.
.P
If
.BR request_key ()
is called from a program invoked by
.BR request_key ()
on behalf of some other process to generate a key, then the keyrings of that
other process will be searched next,
using that other process's user ID, group ID,
supplementary group IDs, and security context to determine access.
.\" David Howells: we can then have an arbitrarily long sequence
.\" of "recursive" request-key upcalls. There is no limit, other
.\" than number of PIDs, etc.
.P
The search of the keyring tree is breadth-first:
the keys in each keyring searched are checked for a match before any child
keyrings are recursed into.
Only keys for which the caller has
.I search
permission be found, and only keyrings for which the caller has
.I search
permission may be searched.
.P
If the key is not found and
.I callout
is NULL, then the call fails with the error
.BR ENOKEY .
If the key is not found and
.I callout
is not NULL, then the kernel attempts to invoke a user-space
program to instantiate the key.
The details are given below.
The
.I dest_keyring
serial number may be that of a valid keyring for which the caller has
.I write
permission, or it may be one of the following special keyring IDs:
.TP
.B KEY_SPEC_THREAD_KEYRING
This specifies the caller's thread-specific keyring
.RB ( thread-keyring (7)).
.TP
.B KEY_SPEC_PROCESS_KEYRING
This specifies the caller's process-specific keyring
.RB ( process-keyring (7)).
.TP
.B KEY_SPEC_SESSION_KEYRING
This specifies the caller's session-specific keyring
.RB ( session-keyring (7)).
.TP
.B KEY_SPEC_USER_KEYRING
This specifies the caller's UID-specific keyring
.RB ( user-keyring (7)).
.TP
.B KEY_SPEC_USER_SESSION_KEYRING
This specifies the caller's UID-session keyring
.RB ( user-session-keyring (7)).
request_key.2: Add information regarding default keyring Notes from Eugene: Based on linux v4.9-rc6 (9c763584): * security/keys/keyctl.c, SYSCALL_DEFINE4(request_key, ...), line 158: * Assume that call is performed with with destringid == 0: * We skip check on line 196, so dest_ref remains NULL * On line 213, request_key_and_link is called with key_ref_to_ptr(dest_ref) * key_ref_to_ptr() itself just zeroes lower bit which is used for indication that key reference in the possession of the current context. * security/keys/request_key.c, request_key_and_link, line 508: * On line 543, we try to search process keyrings for the key (we fill ctx at hte beginning of the function and then pass it to search_process_keyrings) * If key is found (key_ref is not erroneous), we convert key_ref to ptr on line 546 and skip the following block on line 547 since dest_keyring is 0. * If key is not found and error is not EAGAIN, then construct_key_and_link is called on line 566 with dest_keyring == NULL. * security/keys/request_key.c, construct_key_and_link, line 430: * On line 450, construct_get_dest_keyring is called with dest_keyring == NULL. * security/keys/request_key.c, construct_get_dest_keyring, line 253: * The argument here (which is pointer to pointer to struct key) is named _dest_keyring, but on line 257 it is dereferenced to local variable dest_keyring (so it stores NULL now). * We re going to the "else" branch (starting from line 266) of check on line 262 * Now we are switching against cred->jit_keyring with the behavour described in the patch. * git grep jit_keyring security/keys reveals that it is assigned inside keyctl_set_reqkey_keyring, security/keys/keyctl.c, line 1257. * keyctl_set_reqkey_keyring is called from SYSCALL_DEFINE5(keyctl, ...), when option passed to keyctl is KEYCTL_SET_REQKEY_KEYRING (line 1652). * Default value for jit_keyring is sort of difficult to find out, since it is inherited, but overall it is explicitly set to KEY_REQKEY_DEFL_THREAD_KEYRING or copied from zeroed-out structures (so it is equal to KEY_REQKEY_DEFL_DEFAULT) which leads to the same behaviour in case the process has not been upcalled by request_key construction. Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2016-11-21 20:59:40 +00:00
.PP
When the
.I dest_keyring
is specified to
.BR 0 ,
and no key construction have been performed,
then no additional linking is done.
Otherwise, if a new key is constructed, it will be linked to the "default"
request_key.2: Add information regarding default keyring Notes from Eugene: Based on linux v4.9-rc6 (9c763584): * security/keys/keyctl.c, SYSCALL_DEFINE4(request_key, ...), line 158: * Assume that call is performed with with destringid == 0: * We skip check on line 196, so dest_ref remains NULL * On line 213, request_key_and_link is called with key_ref_to_ptr(dest_ref) * key_ref_to_ptr() itself just zeroes lower bit which is used for indication that key reference in the possession of the current context. * security/keys/request_key.c, request_key_and_link, line 508: * On line 543, we try to search process keyrings for the key (we fill ctx at hte beginning of the function and then pass it to search_process_keyrings) * If key is found (key_ref is not erroneous), we convert key_ref to ptr on line 546 and skip the following block on line 547 since dest_keyring is 0. * If key is not found and error is not EAGAIN, then construct_key_and_link is called on line 566 with dest_keyring == NULL. * security/keys/request_key.c, construct_key_and_link, line 430: * On line 450, construct_get_dest_keyring is called with dest_keyring == NULL. * security/keys/request_key.c, construct_get_dest_keyring, line 253: * The argument here (which is pointer to pointer to struct key) is named _dest_keyring, but on line 257 it is dereferenced to local variable dest_keyring (so it stores NULL now). * We re going to the "else" branch (starting from line 266) of check on line 262 * Now we are switching against cred->jit_keyring with the behavour described in the patch. * git grep jit_keyring security/keys reveals that it is assigned inside keyctl_set_reqkey_keyring, security/keys/keyctl.c, line 1257. * keyctl_set_reqkey_keyring is called from SYSCALL_DEFINE5(keyctl, ...), when option passed to keyctl is KEYCTL_SET_REQKEY_KEYRING (line 1652). * Default value for jit_keyring is sort of difficult to find out, since it is inherited, but overall it is explicitly set to KEY_REQKEY_DEFL_THREAD_KEYRING or copied from zeroed-out structures (so it is equal to KEY_REQKEY_DEFL_DEFAULT) which leads to the same behaviour in case the process has not been upcalled by request_key construction. Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2016-11-21 20:59:40 +00:00
keyring (which can be specified via the
.BR keyctl (2)
command
.BR KEYCTL_SET_REQKEY_KEYRING ).
More specifically, when the kernel tries to determine to which keyring the
newly constructed key should be linked, it tries the following options,
starting from the value set via
request_key.2: Add information regarding default keyring Notes from Eugene: Based on linux v4.9-rc6 (9c763584): * security/keys/keyctl.c, SYSCALL_DEFINE4(request_key, ...), line 158: * Assume that call is performed with with destringid == 0: * We skip check on line 196, so dest_ref remains NULL * On line 213, request_key_and_link is called with key_ref_to_ptr(dest_ref) * key_ref_to_ptr() itself just zeroes lower bit which is used for indication that key reference in the possession of the current context. * security/keys/request_key.c, request_key_and_link, line 508: * On line 543, we try to search process keyrings for the key (we fill ctx at hte beginning of the function and then pass it to search_process_keyrings) * If key is found (key_ref is not erroneous), we convert key_ref to ptr on line 546 and skip the following block on line 547 since dest_keyring is 0. * If key is not found and error is not EAGAIN, then construct_key_and_link is called on line 566 with dest_keyring == NULL. * security/keys/request_key.c, construct_key_and_link, line 430: * On line 450, construct_get_dest_keyring is called with dest_keyring == NULL. * security/keys/request_key.c, construct_get_dest_keyring, line 253: * The argument here (which is pointer to pointer to struct key) is named _dest_keyring, but on line 257 it is dereferenced to local variable dest_keyring (so it stores NULL now). * We re going to the "else" branch (starting from line 266) of check on line 262 * Now we are switching against cred->jit_keyring with the behavour described in the patch. * git grep jit_keyring security/keys reveals that it is assigned inside keyctl_set_reqkey_keyring, security/keys/keyctl.c, line 1257. * keyctl_set_reqkey_keyring is called from SYSCALL_DEFINE5(keyctl, ...), when option passed to keyctl is KEYCTL_SET_REQKEY_KEYRING (line 1652). * Default value for jit_keyring is sort of difficult to find out, since it is inherited, but overall it is explicitly set to KEY_REQKEY_DEFL_THREAD_KEYRING or copied from zeroed-out structures (so it is equal to KEY_REQKEY_DEFL_DEFAULT) which leads to the same behaviour in case the process has not been upcalled by request_key construction. Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2016-11-21 20:59:40 +00:00
.BR KEYCTL_SET_REQKEY_KEYRING " " keyctl (2)
command until it finds the first available one:
.IP \(bu 3
.\" 8bbf4976b59fc9fc2861e79cab7beb3f6d647640
Requestor keyring (specified via
.BR KEY_REQKEY_DEFL_REQUESTOR_KEYRING ,
since Linux 2.6.29)
.IP \(bu
Thread-specific keyring (specified via
.BR KEY_REQKEY_DEFL_THREAD_KEYRING )
.IP \(bu
Process-specific keyring (specified via
.BR KEY_REQKEY_DEFL_PROCESS_KEYRING )
.IP \(bu
Session-specific keyring (specified via
.BR KEY_REQKEY_DEFL_SESSION_KEYRING )
.IP \(bu
Session keyring for the process's user ID (specified via
.BR KEY_REQKEY_DEFL_USER_SESSION_KEYRING ).
This keyring is expected to always exist.
.IP \(bu
UID-specific keyring (specified via
.BR KEY_REQKEY_DEFL_USER_KEYRING ).
This keyring is also expected to always exist.
.PP
Specifying
.B KEY_REQKEY_DEFL_DEFAULT
leads to starting from the beginning of the list.
.\"
.SS Requesting user-space instantiation of a key
If the kernel cannot find a key matching
.IR type
and
.IR description ,
and
.I callout
is not NULL, then the kernel attempts to invoke a user-space
program to instantiate a key with the given
.IR type
and
.IR description .
In this case, the following steps are performed:
.IP a) 4
The kernel creates an uninstantiated key, U, with the requested
.I type
and
.IR description .
.IP b)
The kernel creates an authorization key, V,
.\" struct request_key_auth, defined in security/keys/internal.h
that refers to the key U and records the facts that the caller of
.BR request_key (2)
is:
.RS
.IP (1) 4
the context in which the key U should be instantiated and secured, and
2016-11-21 20:59:54 +00:00
.IP (2)
the context from which associated key requests may be satisfied.
.RE
.IP
The authorization key is constructed as follows:
.RS
.IP * 3
The key type is
.IR """.request_key_auth""" .
.IP *
The key's UID and GID are the same as the corresponding filesystem IDs
of the requesting process.
.IP *
The key grants
.IR view ,
.IR read ,
and
.IR search
permissions to the key possessor as well as
.IR view
permission for the key user.
.IP *
The description (name) of the key is the hexadecimal
string representing the ID of the key that is to be instantiated
in the requesting program.
.IP *
The payload of the key is taken from the data specified in
.IR callout_info .
.IP *
2016-11-21 21:00:01 +00:00
Internally, the kernel also records the PID of the process that called
.BR request_key (2).
.RE
.IP c)
The kernel creates a process that executes a user-space service such as
.BR request-key (8)
with a new session keyring that contains a link to the authorization key, V.
.\" The request-key(8) program can be invoked in circumstances *other* than
.\" when triggered by request_key(2). For example, upcalls from places such
.\" as the DNS resolver.
This program is supplied with the following command-line arguments:
.RS
.IP [0] 4
The string
.IR """/sbin/request-key""" .
.IP [1]
The string
.I """create"""
(indicating that a key is to be created).
.IP [2]
The ID of the key that is to be instantiated.
.IP [3]
The filesystem UID of the caller of
.BR request_key ().
.IP [4]
The filesystem GID of the caller of
.BR request_key ().
.IP [5]
The ID of the thread keyring of the caller of
.BR request_key ().
This may be zero if that keyring hasn't been created.
.IP [6]
The ID of the process keyring of the caller of
.BR request_key ().
This may be zero if that keyring hasn't been created.
.IP [7]
The ID of the session keyring of the caller of
.BR request_key ().
.RE
.IP
.IR Note :
each of the command-line arguments that is a key ID is encoded in
.IR decimal
(unlike the key IDs shown in
.IR /proc/keys ,
which are shown as hexadecimal values).
.IP d)
The program spawned in the previous step:
.RS
.IP * 3
Assumes the authority to instantiate the key U using the
.BR keyctl (2)
.BR KEYCTL_ASSUME_AUTHORITY
operation (typically via the
.BR keyctl_assume_authority (3)
function).
.IP *
Obtains the callout data from the payload of the authorization key V
(using the
.BR keyctl (2)
.BR KEYCTL_READ
operation (or, more commonly, the
.BR keyctl_read (3)
function) with a key ID value of
.BR KEY_SPEC_REQKEY_AUTH_KEY ).
.IP *
Instantiates the key
(or execs another program that performs that task),
specifying the payload and destination keyring.
(The destination keyring that the requestor specified when calling
.BR request_key ()
can be accessed using the special key ID
.BR KEY_SPEC_REQUESTOR_KEYRING .)
.\" Should an instantiating program be using KEY_SPEC_REQUESTOR_KEYRING?
.\" I couldn't find a use in the keyutils git repo.
.\" According to David Howells:
.\" * This feature is provided, but not used at the moment.
.\" * A key added to that ring is then owned by the requester
Instantiation is performed using the
.BR keyctl (2)
.BR KEYCTL_INSTANTIATE
operation (or, more commonly, the
.BR keyctl_instantiate (3)
function).
At this point, the
.BR request_key (2)
call completes, and the requesting program can continue execution.
.RE
.P
If these steps are unsuccessful, then an
.BR ENOKEY
error will be returned to the caller of
.BR request_key ()
and a temporary negative key will be installed in the keyring specified by
.IR dest_keyring .
This will expire after a few seconds, but will cause subsequent calls to
.BR request_key ()
to fail until it does.
The purpose of this negatively instantiated key is to prevent
(possibly different) processes making repeated requests
(that require expensive
.BR request-key (8)
upcalls) for a key that can't (at the moment) be positively instantiated.
Once the key has been instantiated, the authorization key
.RB ( KEY_SPEC_REQKEY_AUTH_KEY )
is revoked, and the destination keyring
.RB ( KEY_SPEC_REQUESTOR_KEYRING )
is no longer accessible from the
.BR request-key (8)
program.
If a key is created, then\(emregardless of whether it is a valid key or
a negative key\(emit will displace any other key with
the same type and description from the keyring specified in
.IR dest_keyring .
.SH RETURN VALUE
On success,
.BR request_key ()
returns the serial number of the key it found or caused to be created.
On error, \-1 is returned and
.I errno
is set to indicate the cause of the error.
.SH ERRORS
.TP
.B EACCES
The keyring wasn't available for modification by the user.
.TP
.B EDQUOT
The key quota for this user would be exceeded by creating this key or linking
it to the keyring.
.TP
.B EFAULT
One of
.IR type ,
.IR description ,
or
.IR callout_info
points outside the process's accessible address space.
.TP
.B EINTR
The request was interrupted by a signal; see
.BR signal (7).
.TP
.B EINVAL
The size of the string (including the terminating null byte) specified in
.I type
or
.I description
exceeded the limit (32 bytes and 4096 bytes respectively).
.TP
.B EINVAL
The size of the string (including the terminating null byte) specified in
.I callout_info
exceeded the system page size.
.TP
.B EKEYEXPIRED
An expired key was found, but no replacement could be obtained.
.TP
.B EKEYREJECTED
The attempt to generate a new key was rejected.
.TP
.B EKEYREVOKED
A revoked key was found, but no replacement could be obtained.
.TP
.B ENOKEY
No matching key was found.
.TP
.B ENOMEM
Insufficient memory to create a key.
.TP
.B EPERM
The
.I type
argument started with a period (\(aq.\(aq).
.SH VERSIONS
This system call first appeared in Linux 2.6.10.
The ability to instantiate keys upon request was added
.\" commit 3e30148c3d524a9c1c63ca28261bc24c457eb07a
in Linux 2.6.13.
.SH CONFORMING TO
This system call is a nonstandard Linux extension.
.SH NOTES
No wrapper for this system call is provided in glibc.
A wrapper is provided in the
.IR libkeyutils
package.
When employing the wrapper in that library, link with
.IR \-lkeyutils .
.SH EXAMPLE
The program below demonstrates the use of
.BR request_key ().
The
.IR type ,
.IR description ,
and
.IR callout_info
arguments for the system call are taken from the values
supplied in the command-line arguments.
The call specifies the session keyring as the target keyring.
In order to demonstrate this program,
we first create a suitable entry in the file
.IR /etc/request-key.conf .
.in +4n
.nf
$ sudo sh
# \fBecho 'create user mtk:* * /bin/keyctl instantiate %k %c %S' \\\fP
\fB> /etc/request-keys.conf\fP
# \fBexit\fP
.fi
.in
This entry specifies that when a new "user" key with the prefix
"mtk:" must be instantiated, that task should be performed via the
.BR keyctl (1)
command's
.B instantiate
operation.
The arguments supplied to the
.B instantiate
operation are:
the ID of the uninstantiated key
.RI ( %k );
the callout data supplied to the
.BR request_key ()
call
.RI ( %c );
and the session keyring
.RI ( %S )
of the requestor (i.e., the caller of
.BR request_key ()).
See
.BR request-key.conf (5)
for details of these
.I %
specifiers.
Then we run the program and check the contents of
.IR /proc/keys
to verify that the requested kay has been instantiated:
.in +4n
.nf
$ \fB./t_request_key user mtk:key1 "Payload data"\fP
$ \fBgrep \(aq2dddaf50\(aq /proc/keys\fP
2dddaf50 I--Q--- 1 perm 3f010000 1000 1000 user mtk:key1: 12
.fi
.in
For another example of the use of this program, see
.BR keyctl (2).
.SS Program source
\&
.nf
/* t_request_key.c */
#include <sys/types.h>
#include <keyutils.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[])
{
key_serial_t key;
if (argc != 4) {
fprintf(stderr, "Usage: %s type description callout\-data\\n",
argv[0]);
exit(EXIT_FAILURE);
}
key = request_key(argv[1], argv[2], argv[3],
KEY_SPEC_SESSION_KEYRING);
if (key == \-1) {
perror("request_key");
exit(EXIT_FAILURE);
}
printf("Key ID is %lx\\n", (long) key);
exit(EXIT_SUCCESS);
}
.fi
.SH SEE ALSO
.ad l
.nh
.BR keyctl (1),
.BR add_key (2),
.BR keyctl (2),
.BR keyctl (3),
.BR keyrings (7),
.BR keyutils (7),
.BR capabilities (7),
.BR persistent\-keyring (7),
.BR process\-keyring (7),
.BR session\-keyring (7),
.BR thread\-keyring (7),
.BR user\-keyring (7),
.BR user\-session\-keyring (7),
.BR request\-key (8)
The kernel source files
.IR Documentation/security/keys.txt
and
.IR Documentation/security/keys\-request\-key.txt .