Путеводитель по Руководству Linux

  User  |  Syst  |  Libr  |  Device  |  Files  |  Other  |  Admin  |  Head  |



   keyctl    ( 2 )

манипулировать средствами управления ключами ядра (manipulate the kernel's key management facility)

  Name  |  Synopsis  |  Description  |  Return value  |  Error  |  Versions  |  Conforming to  |  Note  |    Examples    |  See also  |

Примеры (Examples)

The program below provide subset of the functionality of the
       request-key(8) program provided by the keyutils package.  For
       informational purposes, the program records various information
       in a log file.

As described in request_key(2), the request-key(8) program is invoked with command-line arguments that describe a key that is to be instantiated. The example program fetches and logs these arguments. The program assumes authority to instantiate the requested key, and then instantiates that key.

The following shell session demonstrates the use of this program. In the session, we compile the program and then use it to temporarily replace the standard request-key(8) program. (Note that temporarily disabling the standard request-key(8) program may not be safe on some systems.) While our example program is installed, we use the example program shown in request_key(2) to request a key.

$ cc -o key_instantiate key_instantiate.c -lkeyutils $ sudo mv /sbin/request-key /sbin/request-key.backup $ sudo cp key_instantiate /sbin/request-key $ ./t_request_key user mykey somepayloaddata Key ID is 20d035bf $ sudo mv /sbin/request-key.backup /sbin/request-key

Looking at the log file created by this program, we can see the command-line arguments supplied to our example program:

$ cat /tmp/key_instantiate.log Time: Mon Nov 7 13:06:47 2016

Command line arguments: argv[0]: /sbin/request-key operation: create key_to_instantiate: 20d035bf UID: 1000 GID: 1000 thread_keyring: 0 process_keyring: 0 session_keyring: 256e6a6

Key description: user;1000;1000;3f010000;mykey Auth key payload: somepayloaddata Destination keyring: 256e6a6 Auth key description: .request_key_auth;1000;1000;0b010000;20d035bf

The last few lines of the above output show that the example program was able to fetch:

* the description of the key to be instantiated, which included the name of the key (mykey);

* the payload of the authorization key, which consisted of the data (somepayloaddata) passed to request_key(2);

* the destination keyring that was specified in the call to request_key(2); and

* the description of the authorization key, where we can see that the name of the authorization key matches the ID of the key that is to be instantiated (20d035bf).

The example program in request_key(2) specified the destination keyring as KEY_SPEC_SESSION_KEYRING. By examining the contents of /proc/keys, we can see that this was translated to the ID of the destination keyring (0256e6a6) shown in the log output above; we can also see the newly created key with the name mykey and ID 20d035bf.

$ cat /proc/keys | egrep 'mykey|256e6a6' 0256e6a6 I--Q--- 194 perm 3f030000 1000 1000 keyring _ses: 3 20d035bf I--Q--- 1 perm 3f010000 1000 1000 user mykey: 16

Program source

/* key_instantiate.c */

#include <sys/types.h> #include <keyutils.h> #include <time.h> #include <fcntl.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h>

#ifndef KEY_SPEC_REQUESTOR_KEYRING #define KEY_SPEC_REQUESTOR_KEYRING -8 #endif

int main(int argc, char *argv[]) { FILE *fp; time_t t; char *operation; key_serial_t key_to_instantiate, dest_keyring; key_serial_t thread_keyring, process_keyring, session_keyring; uid_t uid; gid_t gid; char dbuf[256]; char auth_key_payload[256]; int akp_size; /* Size of auth_key_payload */ int auth_key;

fp = fopen("/tmp/key_instantiate.log", "w"); if (fp == NULL) exit(EXIT_FAILURE);

setbuf(fp, NULL);

t = time(NULL); fprintf(fp, "Time: %s\n", ctime(&t));

/* * The kernel passes a fixed set of arguments to the program * that it execs; fetch them. */ operation = argv[1]; key_to_instantiate = atoi(argv[2]); uid = atoi(argv[3]); gid = atoi(argv[4]); thread_keyring = atoi(argv[5]); process_keyring = atoi(argv[6]); session_keyring = atoi(argv[7]);

fprintf(fp, "Command line arguments:\n"); fprintf(fp, " argv[0]: %s\n", argv[0]); fprintf(fp, " operation: %s\n", operation); fprintf(fp, " key_to_instantiate: %jx\n", (uintmax_t) key_to_instantiate); fprintf(fp, " UID: %jd\n", (intmax_t) uid); fprintf(fp, " GID: %jd\n", (intmax_t) gid); fprintf(fp, " thread_keyring: %jx\n", (uintmax_t) thread_keyring); fprintf(fp, " process_keyring: %jx\n", (uintmax_t) process_keyring); fprintf(fp, " session_keyring: %jx\n", (uintmax_t) session_keyring); fprintf(fp, "\n");

/* * Assume the authority to instantiate the key named in argv[2]. */ if (keyctl(KEYCTL_ASSUME_AUTHORITY, key_to_instantiate) == -1) { fprintf(fp, "KEYCTL_ASSUME_AUTHORITY failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); }

/* * Fetch the description of the key that is to be instantiated. */ if (keyctl(KEYCTL_DESCRIBE, key_to_instantiate, dbuf, sizeof(dbuf)) == -1) { fprintf(fp, "KEYCTL_DESCRIBE failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); }

fprintf(fp, "Key description: %s\n", dbuf);

/* * Fetch the payload of the authorization key, which is * actually the callout data given to request_key(). */ akp_size = keyctl(KEYCTL_READ, KEY_SPEC_REQKEY_AUTH_KEY, auth_key_payload, sizeof(auth_key_payload)); if (akp_size == -1) { fprintf(fp, "KEYCTL_READ failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); }

auth_key_payload[akp_size] = '\0'; fprintf(fp, "Auth key payload: %s\n", auth_key_payload);

/* * For interest, get the ID of the authorization key and * display it. */ auth_key = keyctl(KEYCTL_GET_KEYRING_ID, KEY_SPEC_REQKEY_AUTH_KEY); if (auth_key == -1) { fprintf(fp, "KEYCTL_GET_KEYRING_ID failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); }

fprintf(fp, "Auth key ID: %jx\n", (uintmax_t) auth_key);

/* * Fetch key ID for the request_key(2) destination keyring. */ dest_keyring = keyctl(KEYCTL_GET_KEYRING_ID, KEY_SPEC_REQUESTOR_KEYRING); if (dest_keyring == -1) { fprintf(fp, "KEYCTL_GET_KEYRING_ID failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); }

fprintf(fp, "Destination keyring: %jx\n", (uintmax_t) dest_keyring);

/* * Fetch the description of the authorization key. This * allows us to see the key type, UID, GID, permissions, * and description (name) of the key. Among other things, * we will see that the name of the key is a hexadecimal * string representing the ID of the key to be instantiated. */ if (keyctl(KEYCTL_DESCRIBE, KEY_SPEC_REQKEY_AUTH_KEY, dbuf, sizeof(dbuf)) == -1) { fprintf(fp, "KEYCTL_DESCRIBE failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); }

fprintf(fp, "Auth key description: %s\n", dbuf);

/* * Instantiate the key using the callout data that was supplied * in the payload of the authorization key. */ if (keyctl(KEYCTL_INSTANTIATE, key_to_instantiate, auth_key_payload, akp_size + 1, dest_keyring) == -1) { fprintf(fp, "KEYCTL_INSTANTIATE failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); }

exit(EXIT_SUCCESS); }