|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
| 2 | +/* |
| 3 | + * Copyright (C) 2019 Microsoft Corporation |
| 4 | + * |
| 5 | + * Author: Lakshmi Ramasubramanian ([email protected]) |
| 6 | + * |
| 7 | + * File: ima_asymmetric_keys.c |
| 8 | + * Defines an IMA hook to measure asymmetric keys on key |
| 9 | + * create or update. |
| 10 | + */ |
| 11 | + |
| 12 | +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | + |
| 14 | +#include <keys/asymmetric-type.h> |
| 15 | +#include "ima.h" |
| 16 | + |
| 17 | +/** |
| 18 | + * ima_post_key_create_or_update - measure asymmetric keys |
| 19 | + * @keyring: keyring to which the key is linked to |
| 20 | + * @key: created or updated key |
| 21 | + * @payload: The data used to instantiate or update the key. |
| 22 | + * @payload_len: The length of @payload. |
| 23 | + * @flags: key flags |
| 24 | + * @create: flag indicating whether the key was created or updated |
| 25 | + * |
| 26 | + * Keys can only be measured, not appraised. |
| 27 | + * The payload data used to instantiate or update the key is measured. |
| 28 | + */ |
| 29 | +void ima_post_key_create_or_update(struct key *keyring, struct key *key, |
| 30 | + const void *payload, size_t payload_len, |
| 31 | + unsigned long flags, bool create) |
| 32 | +{ |
| 33 | + /* Only asymmetric keys are handled by this hook. */ |
| 34 | + if (key->type != &key_type_asymmetric) |
| 35 | + return; |
| 36 | + |
| 37 | + if (!payload || (payload_len == 0)) |
| 38 | + return; |
| 39 | + |
| 40 | + /* |
| 41 | + * keyring->description points to the name of the keyring |
| 42 | + * (such as ".builtin_trusted_keys", ".ima", etc.) to |
| 43 | + * which the given key is linked to. |
| 44 | + * |
| 45 | + * The name of the keyring is passed in the "eventname" |
| 46 | + * parameter to process_buffer_measurement() and is set |
| 47 | + * in the "eventname" field in ima_event_data for |
| 48 | + * the key measurement IMA event. |
| 49 | + */ |
| 50 | + process_buffer_measurement(payload, payload_len, |
| 51 | + keyring->description, KEY_CHECK, 0); |
| 52 | +} |
0 commit comments