/* * Internal function to prefix separator characters in input buffer with escape * character, so that they don't interfere with the construction of key-value pairs, * and clients can split the key1=val1,key2=val2,key3=val3; pairs properly.
*/ staticvoid fix_separator_chars(char **buf)
{ int l = strlen(*buf); int i, j, sp = 0;
for (i = 0; i < l; i++) if ((*buf)[i] == '\\' || (*buf)[i] == ';' || (*buf)[i] == '=' || (*buf)[i] == ',')
sp++;
if (!sp) return;
for (i = l-1, j = i+sp; i >= 0; i--) {
(*buf)[j--] = (*buf)[i]; if ((*buf)[i] == '\\' || (*buf)[i] == ';' || (*buf)[i] == '=' || (*buf)[i] == ',')
(*buf)[j--] = '\\';
}
}
/* * Internal function to allocate memory for IMA measurements.
*/ staticvoid *dm_ima_alloc(size_t len, gfp_t flags, bool noio)
{ unsignedint noio_flag; void *ptr;
if (noio)
noio_flag = memalloc_noio_save();
ptr = kzalloc(len, flags);
if (noio)
memalloc_noio_restore(noio_flag);
return ptr;
}
/* * Internal function to allocate and copy name and uuid for IMA measurements.
*/ staticint dm_ima_alloc_and_copy_name_uuid(struct mapped_device *md, char **dev_name, char **dev_uuid, bool noio)
{ int r;
*dev_name = dm_ima_alloc(DM_NAME_LEN*2, GFP_KERNEL, noio); if (!(*dev_name)) {
r = -ENOMEM; goto error;
}
*dev_uuid = dm_ima_alloc(DM_UUID_LEN*2, GFP_KERNEL, noio); if (!(*dev_uuid)) {
r = -ENOMEM; goto error;
}
r = dm_copy_name_and_uuid(md, *dev_name, *dev_uuid); if (r) goto error;
/* * Internal function to allocate and copy device data for IMA measurements.
*/ staticint dm_ima_alloc_and_copy_device_data(struct mapped_device *md, char **device_data, unsignedint num_targets, bool noio)
{ char *dev_name = NULL, *dev_uuid = NULL; int r;
r = dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio); if (r) return r;
*device_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN, GFP_KERNEL, noio); if (!(*device_data)) {
r = -ENOMEM; goto error;
}
/* * Internal function to allocate and copy current device capacity for IMA measurements.
*/ staticint dm_ima_alloc_and_copy_capacity_str(struct mapped_device *md, char **capacity_str, bool noio)
{
sector_t capacity;
capacity = get_capacity(md->disk);
*capacity_str = dm_ima_alloc(DM_IMA_DEVICE_CAPACITY_BUF_LEN, GFP_KERNEL, noio); if (!(*capacity_str)) return -ENOMEM;
for (i = 0; i < num_targets; i++) { struct dm_target *ti = dm_table_get_target(table, i);
last_target_measured = 0;
/* * First retrieve the target metadata.
*/
target_metadata_buf_len =
scnprintf(target_metadata_buf,
DM_IMA_TARGET_METADATA_BUF_LEN, "target_index=%d,target_begin=%llu,target_len=%llu,",
i, ti->begin, ti->len);
/* * Then retrieve the actual target data.
*/ if (ti->type->status)
ti->type->status(ti, type, status_flags, target_data_buf,
DM_IMA_TARGET_DATA_BUF_LEN); else
target_data_buf[0] = '\0';
target_data_buf_len = strlen(target_data_buf);
/* * Check if the total data can fit into the IMA buffer.
*/
cur_total_buf_len = l + target_metadata_buf_len + target_data_buf_len;
/* * IMA measurements for DM targets are best-effort. * If the total data buffered so far, including the current target, * is too large to fit into DM_IMA_MEASUREMENT_BUF_LEN, measure what * we have in the current buffer, and continue measuring the remaining * targets by prefixing the device metadata again.
*/ if (unlikely(cur_total_buf_len >= DM_IMA_MEASUREMENT_BUF_LEN)) {
dm_ima_measure_data(table_load_event_name, ima_buf, l, noio);
r = crypto_shash_update(shash, (const u8 *)ima_buf, l); if (r < 0) goto error;
memset(ima_buf, 0, DM_IMA_MEASUREMENT_BUF_LEN);
l = 0;
/* * Each new "dm_table_load" entry in IMA log should have device data * prefix, so that multiple records from the same "dm_table_load" for * a given device can be linked together.
*/
memcpy(ima_buf + l, DM_IMA_VERSION_STR, table->md->ima.dm_version_str_len);
l += table->md->ima.dm_version_str_len;
memcpy(ima_buf + l, device_data_buf, device_data_buf_len);
l += device_data_buf_len;
/* * If this iteration of the for loop turns out to be the last target * in the table, dm_ima_measure_data("dm_table_load", ...) doesn't need * to be called again, just the hash needs to be finalized. * "last_target_measured" tracks this state.
*/
last_target_measured = 1;
}
/* * Fill-in all the target metadata, so that multiple targets for the same * device can be linked together.
*/
memcpy(ima_buf + l, target_metadata_buf, target_metadata_buf_len);
l += target_metadata_buf_len;
memcpy(ima_buf + l, target_data_buf, target_data_buf_len);
l += target_data_buf_len;
}
if (!last_target_measured) {
dm_ima_measure_data(table_load_event_name, ima_buf, l, noio);
r = crypto_shash_update(shash, (const u8 *)ima_buf, l); if (r < 0) goto error;
}
/* * Finalize the table hash, and store it in table->md->ima.inactive_table.hash, * so that the table data can be verified against the future device state change * events, e.g. resume, rename, remove, table-clear etc.
*/
r = crypto_shash_final(shash, digest); if (r < 0) goto error;
if (table->md->ima.active_table.device_metadata !=
table->md->ima.inactive_table.device_metadata)
kfree(table->md->ima.inactive_table.device_metadata);
device_table_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN*2, GFP_KERNEL, noio); if (!device_table_data) gotoexit;
r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio); if (r) {
kfree(device_table_data); gotoexit;
}
memcpy(device_table_data + l, DM_IMA_VERSION_STR, md->ima.dm_version_str_len);
l += md->ima.dm_version_str_len;
if (md->ima.active_table.device_metadata) {
memcpy(device_table_data + l, device_active_str, device_active_len);
l += device_active_len;
memcpy(device_table_data + l, md->ima.active_table.device_metadata,
md->ima.active_table.device_metadata_len);
l += md->ima.active_table.device_metadata_len;
nodata = false;
}
if (md->ima.inactive_table.device_metadata) {
memcpy(device_table_data + l, device_inactive_str, device_inactive_len);
l += device_inactive_len;
memcpy(device_table_data + l, md->ima.inactive_table.device_metadata,
md->ima.inactive_table.device_metadata_len);
l += md->ima.inactive_table.device_metadata_len;
nodata = false;
}
if (md->ima.active_table.hash) {
memcpy(device_table_data + l, active_table_str, active_table_len);
l += active_table_len;
memcpy(device_table_data + l, md->ima.active_table.hash,
md->ima.active_table.hash_len);
l += md->ima.active_table.hash_len;
memcpy(device_table_data + l, ",", 1);
l++;
nodata = false;
}
if (md->ima.inactive_table.hash) {
memcpy(device_table_data + l, inactive_table_str, inactive_table_len);
l += inactive_table_len;
memcpy(device_table_data + l, md->ima.inactive_table.hash,
md->ima.inactive_table.hash_len);
l += md->ima.inactive_table.hash_len;
memcpy(device_table_data + l, ",", 1);
l++;
nodata = false;
} /* * In case both active and inactive tables, and corresponding * device metadata is cleared/missing - record the name and uuid * in IMA measurements.
*/ if (nodata) { if (dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio)) goto error;
l = scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN, "%sname=%s,uuid=%s;device_remove=no_data;",
DM_IMA_VERSION_STR, dev_name, dev_uuid);
}
memcpy(device_table_data + l, remove_all_str, remove_all_len);
l += remove_all_len;
memcpy(device_table_data + l, remove_all ? "y;" : "n;", 2);
l += 2;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.