Fix dm-ima bugs
Subsystems (2)
Benjamin Marzinski
maintainerv2 cover Fix dm-ima bugs
The dm-ima code does not guarantee that the dm_ima_measure_on_* functions will not be called at the same time. Since they modify and free shared memory, this can lead to Use-After-Free errors or garbage measurements. Further, they don't make sure that the state they measure corresponds to the actual device state. For instance if table_load() runs at the same time as do_resume() on a table swap, dm_ima_measure_on_device_resume() can end up thinking the wrong table is active. Or a concurrent dm_hash_rename() and a table swap, can end up with a the new active table still using the old name. This patchset makes sure the the dm-ima function are serialized and report the correct device state.
However, the code is still messier that in could be. This is because it duplicates the current measurement events and format. I would really like to know if that is necessary. Specifically, it currently measures the following dm device and table actions:
load
clear
rename
resume
remove
I don't see the benefit of reporting changes to the inactive table, or resumes where the device does not change state. From the user's point of view, the device is still the same after these events. At the same time, it doesn't measure device creates if no table was loaded, so you can have situations where the the first measurement for a device is a rename or a remove. A more sensible set of actions to measure would be:
createtable_swap
rename
remove
Also, the measurement format doesn't map well to how dm device's are actually set up, in a way that makes it harder for the code and records extraneous information. First, like I mentioned before, I don't see the benefit of measuring the inactive table. Second, the name, uuid, and major/minor numbers are properties of the device, not it's table (and dm devices can't have partitions, so the minor count will always be 1). I don't see a reason to store and occasinally log this information twice, if there is an active and incative table, and it forces extra coordination between the dm_ima_measure_on_* functions.
I'm wondering it we are stuck with the current events and format, now that this has been released? Or could we bump the version, and change what events we measure, and how we format the output?
Changes in v2:
- 05/10: Switched from using atomics and barriers to using a spinlock to serialize the dm_ima_measure_on_* functions
- 05/10: Fixed typos in comments and commit message
- 06/10: changed no_data string from "table_rename" to "device_rename"
If this patchset is going in largely as it is, I should point out that there are changes to the ima measurements in corner cases, where the data was previously incorrect (as mentioned in the patches). However it's possible, although I don't think very likely, that systems are expecting these incorrect measurements. We could bump the version number because of this possible change in the measurements, but IIUC, since this version number itself is part of the IMA measurements, that would guarantee that systems would get different measurements.
Benjamin Marzinski (10):
dm-ima: remove dm_ima_reset_data()
dm-ima: remove broken last_target_measured logic
dm-ima: Remove status_flags from dm_ima_measure_on_table_load()
dm-ima: don't copy the active table to the inactive table
dm-ima: Fix UAF errors and measuring incorrect context
dm-ima: remove new_map from dm_ima_measure_on_device_clear
dm-ima: Fix issues with dm_ima_measure_on_device_rename
dm-ima: Handle race between rename and table swap
dm-ima: Fail more gracefully in dm_ima_measure_on_*
dm-ima: use active table's size if available
drivers/md/dm-ima.c | 492 +++++++++++++++++++----------------------- drivers/md/dm-ima.h | 68 ++++-- drivers/md/dm-ioctl.c | 146 +++++++++++-- drivers/md/dm.c | 2 +- 4 files changed, 410 insertions(+), 298 deletions(-)
Signature · 1 line
2.53.0
Benjamin Marzinski
maintainerPATCH v2 1/10 dm-ima: remove dm_ima_reset_data()
There's no point in saving the string length of DM_IMA_VERSION_STR. It's a constant, so the compiler will precompute it. dm_create() will already zero out the rest of dm->ima.
--- drivers/md/dm-ima.c | 32 ++++++++++++-------------------- drivers/md/dm-ima.h | 3 --- drivers/md/dm.c | 2 -- 3 files changed, 12 insertions(+), 25 deletions(-)
drivers/md/dm-ima.c +12 -20
@@ -159,15 +159,6 @@ static int dm_ima_alloc_and_copy_capacity_str(struct mapped_device *md, char **c capacity); } -/* -* Initialize/reset the dm ima related data structure variables. -*/ -void dm_ima_reset_data (struct mapped_device *md) -{ -memset (&(md->ima),0 ,sizeof (md->ima)); - md->ima.dm_version_str_len =strlen (DM_IMA_VERSION_STR ); -} -/* * Build up the IMA data for each target, and finally measure. */
@@ -204,8 +195,8 @@ void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_flsha256_init (&hash_ctx); -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,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR )); + l +=strlen (DM_IMA_VERSION_STR ); device_data_buf_len =strlen (device_data_buf);memcpy (ima_buf + l, device_data_buf, device_data_buf_len);
@@ -260,8 +251,8 @@ void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_fl* 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,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR )); + l +=strlen (DM_IMA_VERSION_STR );memcpy (ima_buf + l, device_data_buf, device_data_buf_len); l += device_data_buf_len;
@@ -349,8 +340,8 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap)if (capacity_len <0 ) goto error; -memcpy (device_table_data + l,DM_IMA_VERSION_STR , md->ima.dm_version_str_len); - l += md->ima.dm_version_str_len; +memcpy (device_table_data + l,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR )); + l +=strlen (DM_IMA_VERSION_STR );if (swap) {if (md->ima.active_table.hash != md->ima.inactive_table.hash)
@@ -460,8 +451,8 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all) goto exit; } -memcpy (device_table_data + l,DM_IMA_VERSION_STR , md->ima.dm_version_str_len); - l += md->ima.dm_version_str_len; +memcpy (device_table_data + l,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR )); + l +=strlen (DM_IMA_VERSION_STR );if (md->ima.active_table.device_metadata) {memcpy (device_table_data + l, device_active_str, device_active_len);
@@ -551,7 +542,8 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all)if (md->ima.active_table.hash != md->ima.inactive_table.hash)kfree (md->ima.inactive_table.hash); -dm_ima_reset_data (md); +memset (&md->ima.active_table,0 ,sizeof (md->ima.active_table)); +memset (&md->ima.inactive_table,0 ,sizeof (md->ima.inactive_table));kfree (dev_name);kfree (dev_uuid);
@@ -578,8 +570,8 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md, bool new_map)if (capacity_len <0 ) goto error1; -memcpy (device_table_data + l,DM_IMA_VERSION_STR , md->ima.dm_version_str_len); - l += md->ima.dm_version_str_len; +memcpy (device_table_data + l,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR )); + l +=strlen (DM_IMA_VERSION_STR );if (md->ima.inactive_table.device_metadata_len && md->ima.inactive_table.hash_len) {
drivers/md/dm-ima.h +0 -3
@@ -52,10 +52,8 @@ struct dm_ima_device_table_metadata {struct dm_ima_measurements {struct dm_ima_device_table_metadata active_table;struct dm_ima_device_table_metadata inactive_table; -unsigned int dm_version_str_len; }; -void dm_ima_reset_data (struct mapped_device *md);void dm_ima_measure_on_table_load (struct dm_table *table,unsigned int status_flags);void dm_ima_measure_on_device_resume (struct mapped_device *md,bool swap);void dm_ima_measure_on_device_remove (struct mapped_device *md,bool remove_all);
@@ -64,7 +62,6 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md);#else -static inline void dm_ima_reset_data (struct mapped_device *md) {}static inline void dm_ima_measure_on_table_load (struct dm_table *table,unsigned int status_flags) {}static inline void dm_ima_measure_on_device_resume (struct mapped_device *md,bool swap) {}static inline void dm_ima_measure_on_device_remove (struct mapped_device *md,bool remove_all) {}
drivers/md/dm.c +0 -2
@@ -2546,8 +2546,6 @@ int dm_create(int minor, struct mapped_device **result)if (!md)return -ENXIO ; -dm_ima_reset_data (md); - *result = md;return 0 ; }
Signature · 1 line
2.53.0
Benjamin Marzinski
maintainerPATCH v2 2/10 dm-ima: remove broken last_target_measured logic
In reply to Benjamin MarzinskiWhen it ran out of space for adding more targets to the ima_buf, dm_ima_measure_on_table_load() would measure the dm device early, and then add the rest of the targets and measure it again. last_target_measured was intended to flag the last target measured so that the device wouldn't get remeasured, if no new targets were added after the early measurement. But the way to code works, the dm device will never be measured early unless there is another target to add to the ima_buf. Instead, if there is only one more target to add, that target was getting added to the ima_buf, but it wasn't getting remeasured, because last_target_measured was set. Since dm_ima_measure_on_table_load() only measures a device early when there are more targets to add, the final measurement must always happen, and last_target_measured is unneeded.
--- drivers/md/dm-ima.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-)
drivers/md/dm-ima.c +2 -16
@@ -167,7 +167,6 @@ void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_flsize_t device_data_buf_len, target_metadata_buf_len, target_data_buf_len, l =0 ;char *target_metadata_buf =NULL , *target_data_buf =NULL , *digest_buf =NULL ;char *ima_buf =NULL , *device_data_buf =NULL ; -int last_target_measured =-1 ;status_type_t type =STATUSTYPE_IMA ;size_t cur_total_buf_len =0 ;unsigned int num_targets, i;
@@ -205,8 +204,6 @@ void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_flfor (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. */
@@ -256,14 +253,6 @@ void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_flmemcpy (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 ; }/*
@@ -277,11 +266,8 @@ void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_fl l += target_data_buf_len; } -if (!last_target_measured) { -dm_ima_measure_data (table_load_event_name, ima_buf, l, noio); - -sha256_update (&hash_ctx, (const u8 *)ima_buf, l); - } +dm_ima_measure_data (table_load_event_name, ima_buf, l, noio); +sha256_update (&hash_ctx, (const u8 *)ima_buf, l);/* * Finalize the table hash, and store it in table->md->ima.inactive_table.hash,
Signature · 1 line
2.53.0
Benjamin Marzinski
maintainerPATCH v2 3/10 dm-ima: Remove status_flags from dm_ima_measure_on_table_load()
In reply to Benjamin MarzinskiThere is no status flag that is is used for STATUSTYPE_IMA type status() calls, and STATUSTYPE_IMA itself is not a valid status flag.
--- drivers/md/dm-ima.c | 4 ++-- drivers/md/dm-ima.h | 4 ++-- drivers/md/dm-ioctl.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-)
drivers/md/dm-ima.c +2 -2
@@ -162,7 +162,7 @@ static int dm_ima_alloc_and_copy_capacity_str(struct mapped_device *md, char **c/* * Build up the IMA data for each target, and finally measure. */ -void dm_ima_measure_on_table_load (struct dm_table *table,unsigned int status_flags) +void dm_ima_measure_on_table_load (struct dm_table *table) {size_t device_data_buf_len, target_metadata_buf_len, target_data_buf_len, l =0 ;char *target_metadata_buf =NULL , *target_data_buf =NULL , *digest_buf =NULL ;
@@ -217,7 +217,7 @@ void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_fl* Then retrieve the actual target data. */ if (ti->type->status) - ti->type->status (ti, type, status_flags, target_data_buf, + ti->type->status (ti, type,0 , target_data_buf,DM_IMA_TARGET_DATA_BUF_LEN );else target_data_buf[0 ] ='\0' ;
drivers/md/dm-ima.h +2 -2
@@ -54,7 +54,7 @@ struct dm_ima_measurements {struct dm_ima_device_table_metadata inactive_table; }; -void dm_ima_measure_on_table_load (struct dm_table *table,unsigned int status_flags); +void dm_ima_measure_on_table_load (struct dm_table *table);void dm_ima_measure_on_device_resume (struct mapped_device *md,bool swap);void dm_ima_measure_on_device_remove (struct mapped_device *md,bool remove_all);void dm_ima_measure_on_table_clear (struct mapped_device *md,bool new_map);
@@ -62,7 +62,7 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md);#else -static inline void dm_ima_measure_on_table_load (struct dm_table *table,unsigned int status_flags) {} +static inline void dm_ima_measure_on_table_load (struct dm_table *table) {}static inline void dm_ima_measure_on_device_resume (struct mapped_device *md,bool swap) {}static inline void dm_ima_measure_on_device_remove (struct mapped_device *md,bool remove_all) {}static inline void dm_ima_measure_on_table_clear (struct mapped_device *md,bool new_map) {}
drivers/md/dm-ioctl.c +1 -1
@@ -1552,7 +1552,7 @@ static int table_load(struct file *filp, struct dm_ioctl *param, size_t param_siif (r) goto err_unlock_md_type; -dm_ima_measure_on_table_load (t,STATUSTYPE_IMA ); +dm_ima_measure_on_table_load (t); immutable_target_type =dm_get_immutable_target_type (md); if (immutable_target_type &&
Signature · 1 line
2.53.0
Benjamin Marzinski
maintainerPATCH v2 4/10 dm-ima: don't copy the active table to the inactive table
In reply to Benjamin MarzinskiIf an inactive table was cleared, dm_ima_measure_on_table_clear() was copying the ima.active_table to ima.inactive_table. This is not what device-mapper does, and it makes the IMA measurements show an inactive table when there isn't one. Also, once this is removed, the code no longer needs to keep checking if the active and the inactive table point to the same memory.
--- drivers/md/dm-ima.c | 64 +++++++-------------------------------------- 1 file changed, 10 insertions(+), 54 deletions(-)
drivers/md/dm-ima.c +10 -54
@@ -281,17 +281,13 @@ void dm_ima_measure_on_table_load(struct dm_table *table)if (!digest_buf) goto error; -if (table->md->ima.active_table.hash != table->md->ima.inactive_table.hash) -kfree (table->md->ima.inactive_table.hash); - +kfree (table->md->ima.inactive_table.hash); table->md->ima.inactive_table.hash = digest_buf; table->md->ima.inactive_table.hash_len =strlen (digest_buf); table->md->ima.inactive_table.num_targets = num_targets; -if (table->md->ima.active_table.device_metadata != - table->md->ima.inactive_table.device_metadata) -kfree (table->md->ima.inactive_table.device_metadata); +kfree (table->md->ima.inactive_table.device_metadata); table->md->ima.inactive_table.device_metadata = device_data_buf; table->md->ima.inactive_table.device_metadata_len = device_data_buf_len;
@@ -330,19 +326,9 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap) l +=strlen (DM_IMA_VERSION_STR );if (swap) { -if (md->ima.active_table.hash != md->ima.inactive_table.hash) -kfree (md->ima.active_table.hash); - - md->ima.active_table.hash =NULL ; - md->ima.active_table.hash_len =0 ; - -if (md->ima.active_table.device_metadata != - md->ima.inactive_table.device_metadata) -kfree (md->ima.active_table.device_metadata); - - md->ima.active_table.device_metadata =NULL ; - md->ima.active_table.device_metadata_len =0 ; - md->ima.active_table.num_targets =0 ; +kfree (md->ima.active_table.hash); +kfree (md->ima.active_table.device_metadata); +memset (&md->ima.active_table,0 ,sizeof (md->ima.active_table));if (md->ima.inactive_table.hash) { md->ima.active_table.hash = md->ima.inactive_table.hash;
@@ -518,15 +504,10 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all)kfree (capacity_str); exit:kfree (md->ima.active_table.device_metadata); - -if (md->ima.active_table.device_metadata != - md->ima.inactive_table.device_metadata) -kfree (md->ima.inactive_table.device_metadata); +kfree (md->ima.inactive_table.device_metadata);kfree (md->ima.active_table.hash); - -if (md->ima.active_table.hash != md->ima.inactive_table.hash) -kfree (md->ima.inactive_table.hash); +kfree (md->ima.inactive_table.hash);memset (&md->ima.active_table,0 ,sizeof (md->ima.active_table));memset (&md->ima.inactive_table,0 ,sizeof (md->ima.inactive_table));
@@ -594,34 +575,9 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md, bool new_map)dm_ima_measure_data ("dm_table_clear" , device_table_data, l, noio);if (new_map) { -if (md->ima.inactive_table.hash && - md->ima.inactive_table.hash != md->ima.active_table.hash) -kfree (md->ima.inactive_table.hash); - - md->ima.inactive_table.hash =NULL ; - md->ima.inactive_table.hash_len =0 ; - -if (md->ima.inactive_table.device_metadata && - md->ima.inactive_table.device_metadata != md->ima.active_table.device_metadata) -kfree (md->ima.inactive_table.device_metadata); - - md->ima.inactive_table.device_metadata =NULL ; - md->ima.inactive_table.device_metadata_len =0 ; - md->ima.inactive_table.num_targets =0 ; - -if (md->ima.active_table.hash) { - md->ima.inactive_table.hash = md->ima.active_table.hash; - md->ima.inactive_table.hash_len = md->ima.active_table.hash_len; - } - -if (md->ima.active_table.device_metadata) { - md->ima.inactive_table.device_metadata = - md->ima.active_table.device_metadata; - md->ima.inactive_table.device_metadata_len = - md->ima.active_table.device_metadata_len; - md->ima.inactive_table.num_targets = - md->ima.active_table.num_targets; - } +kfree (md->ima.inactive_table.hash); +kfree (md->ima.inactive_table.device_metadata); +memset (&md->ima.inactive_table,0 ,sizeof (md->ima.inactive_table)); }kfree (dev_name);
Signature · 1 line
2.53.0
Benjamin Marzinski
maintainerPATCH v2 5/10 dm-ima: Fix UAF errors and measuring incorrect context
In reply to Benjamin Marzinskithe dm-ima code did not keep the dm_ima_measure_on_* functions from running at the same time. This could lead to various errors. If two processes were updating the device state, one could update the state first, but the other could measure the state first, causing the the current device state to appear incorrect. If a table load happened while a device was resuming, the IMA measurement could report the wrong table being active. And if two dm_ima_measure_on_* functions ran at the same time, one of them could free data that the other was accessing, causing a crash.
All the core dm functions that call a dm_ima_measure_on_* function update the device state they want to measure under the _hash_lock, except for do_resume(). But holding the _hash_lock is not a good way to synchronize these functions. It's a global mutex, that is needed in many dm operations, and the dm_ima_measure_* functions can sleep, blocking any dm operation on any device that needs the _hash_lock.
To serialize and order the IMA measurement functions, the dm_ima_measurements now has two counters, update_idx and measure_idx. update_idx is incremented while holding the _hash_lock and saved, along with the device name and uuid, in a dm_ima_context struct. Once the _hash_lock is dropped, the dm_ima_measure_* function is called. It waits until measure_idx matches the saved value of update_idx, ensuring that the updates and measurements happen in the same order if there are multiple processes changing the device at the same time. Then it measures the device, updates measure_idx, and wakes up any other process waiting to do a measurement. This makes sure that the measurements are serialized and done in the order that the _hash_lock was acquired in. But they only block other measurements for the same device, which are unlikely to happen at the same time.
do_resume() is trickier, because it removes the inactive table while holding the _hash_lock, but doesn't hold it while updating md->map. To make sure it is also ordered, the IMA code grabs the _hash_lock after md->map is updated. Then it makes sure that the device isn't being removed and that another do_resume() hasn't already changed the active table again, and serializes like the other functions do.
--- drivers/md/dm-ima.c | 259 ++++++++++++++++++++++++------------------ drivers/md/dm-ima.h | 65 +++++++++-- drivers/md/dm-ioctl.c | 144 +++++++++++++++++++++-- drivers/md/dm.c | 2 + 4 files changed, 340 insertions(+), 130 deletions(-)
drivers/md/dm-ima.c +149 -110
@@ -21,25 +21,32 @@* 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. */ -static void fix_separator_chars (char **buf) +static void fix_separator_chars (char *buf) { -int l =strlen (*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] ==',' ) +if (buf[i] =='\\' || buf[i] ==';' || buf[i] =='=' || buf[i] ==',' ) sp++;if (!sp)return ; + buf[l + sp] ='\0' ;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--] ='\\' ; + buf[j--] = buf[i]; +if (buf[i] =='\\' || buf[i] ==';' || buf[i] =='=' || buf[i] ==',' ) + buf[j--] ='\\' ; } } +static void fix_context_strings (struct dm_ima_context *context) +{ +fix_separator_chars (context->dev_name); +fix_separator_chars (context->dev_uuid); +} +/* * Internal function to allocate memory for IMA measurements. */
@@ -59,68 +66,89 @@ static void *dm_ima_alloc(size_t len, bool noio)return ptr; } +void dm_ima_init (struct mapped_device *md) +{ + md->ima.update_idx =0 ; + md->ima.measure_idx =0 ; +init_waitqueue_head (&md->ima.ima_wq); +spin_lock_init (&md->ima.ima_lock); +} + +void dm_ima_alloc_context (struct dm_ima_context **context,bool noio) +{ + *context =dm_ima_alloc (sizeof (struct dm_ima_context ), noio); +} + +void dm_ima_free_context (struct dm_ima_context *context) +{ +if (likely (context)) { +kfree (context->table.device_metadata); +kfree (context->table.hash); +kfree (context); + } +} + +static void wait_to_measure (struct dm_ima_measurements *ima, +unsigned int update_idx) +{ +spin_lock_irq (&ima->ima_lock); +wait_event_lock_irq (ima->ima_wq, + ima->measure_idx == update_idx, + ima->ima_lock); +spin_unlock_irq (&ima->ima_lock); +} + +static void wake_next_measure (struct dm_ima_measurements *ima) +{ +spin_lock_irq (&ima->ima_lock); + ima->measure_idx++; +spin_unlock_irq (&ima->ima_lock); +wake_up_all (&ima->ima_wq); +} +/* -* Internal function to allocate and copy name and uuid for IMA measurements. +* Helper function for swapping the table, to make sure that the +* correct table metadata is saved and restored. */ -static int dm_ima_alloc_and_copy_name_uuid (struct mapped_device *md,char **dev_name, -char **dev_uuid,bool noio) +void dm_ima_context_table_op (struct mapped_device *md, +struct dm_ima_context *context, +enum dm_ima_table_op op) { -int r; - *dev_name =dm_ima_alloc (DM_NAME_LEN *2 , noio); -if (!(*dev_name)) { - r = -ENOMEM ; - goto error; - } +struct dm_ima_measurements *ima = &md->ima; - *dev_uuid =dm_ima_alloc (DM_UUID_LEN *2 , noio); -if (!(*dev_uuid)) { - r = -ENOMEM ; - goto error; - } +if (unlikely (!context)) +return ; - r =dm_copy_name_and_uuid (md, *dev_name, *dev_uuid); -if (r) - goto error; +wait_to_measure (ima, context->update_idx); -fix_separator_chars (dev_name); -fix_separator_chars (dev_uuid); +if (op ==DM_IMA_TABLE_SAVE ) { + context->table = ima->inactive_table; +memset (&ima->inactive_table,0 ,sizeof (ima->inactive_table)); + }else { + ima->inactive_table = context->table; +memset (&context->table,0 ,sizeof (context->table)); + } -return 0 ; -error: -kfree (*dev_name); -kfree (*dev_uuid); - *dev_name =NULL ; - *dev_uuid =NULL ; -return r; +wake_next_measure (ima); }/* * Internal function to allocate and copy device data for IMA measurements. */ static int dm_ima_alloc_and_copy_device_data (struct mapped_device *md,char **device_data, +struct dm_ima_context *context,unsigned int 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 , noio); -if (!(*device_data)) { - r = -ENOMEM ; - goto error; - } +if (!(*device_data)) +return -ENOMEM ;scnprintf (*device_data,DM_IMA_DEVICE_BUF_LEN ,"name=%s,uuid=%s,major=%d,minor=%d,minor_count=%d,num_targets=%u;" , - dev_name, dev_uuid, md->disk->major, md->disk->first_minor, - md->disk->minors, num_targets); -error: -kfree (dev_name); -kfree (dev_uuid); -return r; + context->dev_name, context->dev_uuid, md->disk->major, + md->disk->first_minor, md->disk->minors, num_targets); + +return 0 ; }/*
@@ -162,7 +190,8 @@ static int dm_ima_alloc_and_copy_capacity_str(struct mapped_device *md, char **c/* * Build up the IMA data for each target, and finally measure. */ -void dm_ima_measure_on_table_load (struct dm_table *table) +void dm_ima_measure_on_table_load (struct dm_table *table, +struct dm_ima_context *context) {size_t device_data_buf_len, target_metadata_buf_len, target_data_buf_len, l =0 ;char *target_metadata_buf =NULL , *target_data_buf =NULL , *digest_buf =NULL ;
@@ -175,9 +204,14 @@ void dm_ima_measure_on_table_load(struct dm_table *table)bool noio = false;char table_load_event_name[] ="dm_table_load" ; +if (unlikely (!context)) +return ; + +wait_to_measure (&table->md->ima, context->update_idx); + ima_buf =dm_ima_alloc (DM_IMA_MEASUREMENT_BUF_LEN , noio);if (!ima_buf) -return ; + goto error; target_metadata_buf =dm_ima_alloc (DM_IMA_TARGET_METADATA_BUF_LEN , noio);if (!target_metadata_buf)
@@ -189,7 +223,9 @@ void dm_ima_measure_on_table_load(struct dm_table *table) num_targets = table->num_targets; -if (dm_ima_alloc_and_copy_device_data (table->md, &device_data_buf, num_targets, noio)) +fix_context_strings (context); +if (dm_ima_alloc_and_copy_device_data (table->md, &device_data_buf, + context, num_targets, noio)) goto error;sha256_init (&hash_ctx);
@@ -299,14 +335,17 @@ void dm_ima_measure_on_table_load(struct dm_table *table)kfree (ima_buf);kfree (target_metadata_buf);kfree (target_data_buf); + +wake_next_measure (&table->md->ima); }/* * Measure IMA data on device resume. */ -void dm_ima_measure_on_device_resume (struct mapped_device *md,bool swap) +void dm_ima_measure_on_device_resume (struct mapped_device *md,bool swap, +struct dm_ima_context *context) { -char *device_table_data, *dev_name =NULL , *dev_uuid =NULL , *capacity_str =NULL ; +char *device_table_data =NULL , *capacity_str =NULL ;char active[] ="active_table_hash=" ;unsigned int active_len =strlen (active);unsigned int l =0 ;
@@ -314,9 +353,14 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap)bool nodata = true;int capacity_len; +if (unlikely (!context)) +return ; + +wait_to_measure (&md->ima, context->update_idx); + device_table_data =dm_ima_alloc (DM_IMA_DEVICE_BUF_LEN , noio);if (!device_table_data) -return ; + goto error; capacity_len =dm_ima_alloc_and_copy_capacity_str (md, &capacity_str, noio);if (capacity_len <0 )
@@ -328,25 +372,8 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap)if (swap) {kfree (md->ima.active_table.hash);kfree (md->ima.active_table.device_metadata); -memset (&md->ima.active_table,0 ,sizeof (md->ima.active_table)); - -if (md->ima.inactive_table.hash) { - md->ima.active_table.hash = md->ima.inactive_table.hash; - md->ima.active_table.hash_len = md->ima.inactive_table.hash_len; - md->ima.inactive_table.hash =NULL ; - md->ima.inactive_table.hash_len =0 ; - } - -if (md->ima.inactive_table.device_metadata) { - md->ima.active_table.device_metadata = - md->ima.inactive_table.device_metadata; - md->ima.active_table.device_metadata_len = - md->ima.inactive_table.device_metadata_len; - md->ima.active_table.num_targets = md->ima.inactive_table.num_targets; - md->ima.inactive_table.device_metadata =NULL ; - md->ima.inactive_table.device_metadata_len =0 ; - md->ima.inactive_table.num_targets =0 ; - } + md->ima.active_table = context->table; +memset (&context->table,0 ,sizeof (context->table)); }if (md->ima.active_table.device_metadata) {
@@ -372,12 +399,11 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap) }if (nodata) { -if (dm_ima_alloc_and_copy_name_uuid (md, &dev_name, &dev_uuid, noio)) - goto error; - +fix_context_strings (context); l =scnprintf (device_table_data,DM_IMA_DEVICE_BUF_LEN ,"%sname=%s,uuid=%s;device_resume=no_data;" , -DM_IMA_VERSION_STR , dev_name, dev_uuid); +DM_IMA_VERSION_STR , context->dev_name, + context->dev_uuid); }memcpy (device_table_data + l, capacity_str, capacity_len);
@@ -385,19 +411,21 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap)dm_ima_measure_data ("dm_device_resume" , device_table_data, l, noio); -kfree (dev_name); -kfree (dev_uuid); error:kfree (capacity_str);kfree (device_table_data); + +wake_next_measure (&md->ima); }/* * Measure IMA data on remove. */ -void dm_ima_measure_on_device_remove (struct mapped_device *md,bool remove_all) +void dm_ima_measure_on_device_remove (struct mapped_device *md,bool remove_all, +struct dm_ima_context *context, +unsigned int idx) { -char *device_table_data, *dev_name =NULL , *dev_uuid =NULL , *capacity_str =NULL ; +char *device_table_data, *capacity_str =NULL ;char active_table_str[] ="active_table_hash=" ;char inactive_table_str[] ="inactive_table_hash=" ;char device_active_str[] ="device_active_metadata=" ;
@@ -413,6 +441,11 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all)bool nodata = true;int capacity_len; +wait_to_measure (&md->ima, idx); + +if (unlikely (!context)) + goto exit; + device_table_data =dm_ima_alloc (DM_IMA_DEVICE_BUF_LEN *2 , noio);if (!device_table_data) goto exit;
@@ -481,12 +514,11 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all)* in IMA measurements. */ if (nodata) { -if (dm_ima_alloc_and_copy_name_uuid (md, &dev_name, &dev_uuid, noio)) - goto error; - +fix_context_strings (context); 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); +DM_IMA_VERSION_STR , context->dev_name, + context->dev_uuid); }memcpy (device_table_data + l, remove_all_str, remove_all_len);
@@ -499,7 +531,6 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all)dm_ima_measure_data ("dm_device_remove" , device_table_data, l, noio); -error:kfree (device_table_data);kfree (capacity_str); exit:
@@ -512,30 +543,35 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all)memset (&md->ima.active_table,0 ,sizeof (md->ima.active_table));memset (&md->ima.inactive_table,0 ,sizeof (md->ima.inactive_table)); -kfree (dev_name); -kfree (dev_uuid); +wake_next_measure (&md->ima); }/* * Measure ima data on table clear. */ -void dm_ima_measure_on_table_clear (struct mapped_device *md,bool new_map) +void dm_ima_measure_on_table_clear (struct mapped_device *md,bool new_map, +struct dm_ima_context *context) {unsigned int l =0 ; -char *device_table_data =NULL , *dev_name =NULL , *dev_uuid =NULL , *capacity_str =NULL ; +char *device_table_data =NULL , *capacity_str =NULL ;char inactive_str[] ="inactive_table_hash=" ;unsigned int inactive_len =strlen (inactive_str);bool noio = true;bool nodata = true;int capacity_len; +if (unlikely (!context)) +return ; + +wait_to_measure (&md->ima, context->update_idx); + device_table_data =dm_ima_alloc (DM_IMA_DEVICE_BUF_LEN , noio);if (!device_table_data) -return ; + goto error; capacity_len =dm_ima_alloc_and_copy_capacity_str (md, &capacity_str, noio);if (capacity_len <0 ) - goto error1; + goto error;memcpy (device_table_data + l,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR )); l +=strlen (DM_IMA_VERSION_STR );
@@ -561,12 +597,11 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md, bool new_map) }if (nodata) { -if (dm_ima_alloc_and_copy_name_uuid (md, &dev_name, &dev_uuid, noio)) - goto error2; - +fix_context_strings (context); l =scnprintf (device_table_data,DM_IMA_DEVICE_BUF_LEN ,"%sname=%s,uuid=%s;table_clear=no_data;" , -DM_IMA_VERSION_STR , dev_name, dev_uuid); +DM_IMA_VERSION_STR , context->dev_name, + context->dev_uuid); }memcpy (device_table_data + l, capacity_str, capacity_len);
@@ -580,29 +615,33 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md, bool new_map)memset (&md->ima.inactive_table,0 ,sizeof (md->ima.inactive_table)); } -kfree (dev_name); -kfree (dev_uuid); -error2: +error:kfree (capacity_str); -error1:kfree (device_table_data); + +wake_next_measure (&md->ima); }/* * Measure IMA data on device rename. */ -void dm_ima_measure_on_device_rename (struct mapped_device *md) +void dm_ima_measure_on_device_rename (struct mapped_device *md, +struct dm_ima_context *context) { -char *old_device_data =NULL , *new_device_data =NULL , *combined_device_data =NULL ; -char *new_dev_name =NULL , *new_dev_uuid =NULL , *capacity_str =NULL ; +char *old_device_data =NULL , *new_device_data =NULL ; +char *combined_device_data =NULL , *capacity_str =NULL ;bool noio = true;int len; -if (dm_ima_alloc_and_copy_device_data (md, &new_device_data, - md->ima.active_table.num_targets, noio)) +if (unlikely (!context))return ; -if (dm_ima_alloc_and_copy_name_uuid (md, &new_dev_name, &new_dev_uuid, noio)) +wait_to_measure (&md->ima, context->update_idx); + +fix_context_strings (context); +if (dm_ima_alloc_and_copy_device_data (md, &new_device_data, context, + md->ima.active_table.num_targets, + noio)) goto error; combined_device_data =dm_ima_alloc (DM_IMA_DEVICE_BUF_LEN *2 , noio);
@@ -619,7 +658,7 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md) len =scnprintf (combined_device_data,DM_IMA_DEVICE_BUF_LEN *2 ,"%s%snew_name=%s,new_uuid=%s;%s" ,DM_IMA_VERSION_STR , old_device_data, - new_dev_name, new_dev_uuid, capacity_str); + context->dev_name, context->dev_uuid, capacity_str);dm_ima_measure_data ("dm_device_rename" , combined_device_data, len, noio);
@@ -631,6 +670,6 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md)kfree (capacity_str);kfree (combined_device_data);kfree (old_device_data); -kfree (new_dev_name); -kfree (new_dev_uuid); + +wake_next_measure (&md->ima); }
drivers/md/dm-ima.h +55 -10
89 lines not on this page · raw message
drivers/md/dm-ioctl.c +134 -10
373 lines not on this page · raw message
drivers/md/dm.c +2 -0
@@ -2546,6 +2546,8 @@ int dm_create(int minor, struct mapped_device **result)if (!md)return -ENXIO ; +dm_ima_init (md); + *result = md;return 0 ; }
Signature · 1 line
2.53.0
Benjamin Marzinski
maintainerPATCH v2 6/10 dm-ima: remove new_map from dm_ima_measure_on_device_clear
In reply to Benjamin MarzinskiNow that two processes can't modify md->ima in dm_ima_measure_on_device_clear() at the same time, there's no need to track if an inactive table was actually removed. We might as well clean it up unconditionally, on the off chance that a previous ima measurement failed and left md->ima.inactive_table behind.
--- drivers/md/dm-ima.c | 10 ++++------ drivers/md/dm-ima.h | 3 +-- drivers/md/dm-ioctl.c | 4 +--- 3 files changed, 6 insertions(+), 11 deletions(-)
drivers/md/dm-ima.c +4 -6
@@ -549,7 +549,7 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all,/* * Measure ima data on table clear. */ -void dm_ima_measure_on_table_clear (struct mapped_device *md,bool new_map, +void dm_ima_measure_on_table_clear (struct mapped_device *md,struct dm_ima_context *context) {unsigned int l =0 ;
@@ -609,11 +609,9 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md, bool new_map,dm_ima_measure_data ("dm_table_clear" , device_table_data, l, noio); -if (new_map) { -kfree (md->ima.inactive_table.hash); -kfree (md->ima.inactive_table.device_metadata); -memset (&md->ima.inactive_table,0 ,sizeof (md->ima.inactive_table)); - } +kfree (md->ima.inactive_table.hash); +kfree (md->ima.inactive_table.device_metadata); +memset (&md->ima.inactive_table,0 ,sizeof (md->ima.inactive_table)); error:kfree (capacity_str);
drivers/md/dm-ima.h +1 -2
@@ -83,7 +83,7 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap,void dm_ima_measure_on_device_remove (struct mapped_device *md,bool remove_all,struct dm_ima_context *context,unsigned int idx); -void dm_ima_measure_on_table_clear (struct mapped_device *md,bool new_map, +void dm_ima_measure_on_table_clear (struct mapped_device *md,struct dm_ima_context *context);void dm_ima_measure_on_device_rename (struct mapped_device *md,struct dm_ima_context *context);
@@ -108,7 +108,6 @@ static inline void dm_ima_measure_on_device_remove(struct mapped_device *md, struct dm_ima_context *context, unsigned int idx) {}static inline void dm_ima_measure_on_table_clear (struct mapped_device *md, -bool new_map,struct dm_ima_context *context) {}static inline void dm_ima_measure_on_device_rename (struct mapped_device *md,struct dm_ima_context *context) {}
drivers/md/dm-ioctl.c +1 -3
@@ -1741,7 +1741,6 @@ static int table_clear(struct file *filp, struct dm_ioctl *param, size_t param_sstruct hash_cell *hc;struct mapped_device *md;struct dm_table *old_map =NULL ; -bool has_new_map = false;struct dm_ima_context *ima_context =NULL ;dm_ima_alloc_context (&ima_context, true);
@@ -1758,13 +1757,12 @@ static int table_clear(struct file *filp, struct dm_ioctl *param, size_t param_sif (hc->new_map) { old_map = hc->new_map; hc->new_map =NULL ; - has_new_map = true; }dm_ima_init_context (hc, ima_context, false); md = hc->md;up_write (&_hash_lock); -dm_ima_measure_on_table_clear (md, has_new_map, ima_context); +dm_ima_measure_on_table_clear (md, ima_context);dm_ima_free_context (ima_context); param->flags &= ~DM_INACTIVE_PRESENT_FLAG ;
Signature · 1 line
2.53.0
Benjamin Marzinski
maintainerPATCH v2 7/10 dm-ima: Fix issues with dm_ima_measure_on_device_rename
In reply to Benjamin Marzinskidm_ima_measure_on_device_rename() can be called on a device before it ever loads a table, so it needs to handle the case where there is no table metadata. Also, it was only updating the table_metadata on the active table. If there was an inactive table when the device was renamed and that table was later swapped in as the active table, it would still have the old name. dm_ima_measure_on_device_rename() was also needlessly allocating new memory for the updated table metadata, instead of just reusing the existing memory.
--- drivers/md/dm-ima.c | 69 ++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 32 deletions(-)
drivers/md/dm-ima.c +37 -32
@@ -133,22 +133,18 @@ void dm_ima_context_table_op(struct mapped_device *md, }/* -* Internal function to allocate and copy device data for IMA measurements. +* Internal function to copy device data for IMA measurements. */ -static int dm_ima_alloc_and_copy_device_data (struct mapped_device *md,char **device_data, -struct dm_ima_context *context, -unsigned int num_targets,bool noio) +static void dm_ima_copy_device_data (struct mapped_device *md,char *device_data, +struct dm_ima_context *context, +unsigned int num_targets) { - *device_data =dm_ima_alloc (DM_IMA_DEVICE_BUF_LEN , noio); -if (!(*device_data)) -return -ENOMEM ; - -scnprintf (*device_data,DM_IMA_DEVICE_BUF_LEN , +memset (device_data,0 ,DM_IMA_DEVICE_BUF_LEN ); +scnprintf (device_data,DM_IMA_DEVICE_BUF_LEN ,"name=%s,uuid=%s,major=%d,minor=%d,minor_count=%d,num_targets=%u;" , context->dev_name, context->dev_uuid, md->disk->major, md->disk->first_minor, md->disk->minors, num_targets); -return 0 ; }/*
@@ -223,11 +219,14 @@ void dm_ima_measure_on_table_load(struct dm_table *table, num_targets = table->num_targets; -fix_context_strings (context); -if (dm_ima_alloc_and_copy_device_data (table->md, &device_data_buf, - context, num_targets, noio)) + device_data_buf =dm_ima_alloc (DM_IMA_DEVICE_BUF_LEN , noio); +if (!device_data_buf) goto error; +fix_context_strings (context); +dm_ima_copy_device_data (table->md, device_data_buf, context, + num_targets); +sha256_init (&hash_ctx);memcpy (ima_buf + l,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR ));
@@ -626,48 +625,54 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md,void dm_ima_measure_on_device_rename (struct mapped_device *md,struct dm_ima_context *context) { -char *old_device_data =NULL , *new_device_data =NULL ; +char *old_device_data =NULL ;char *combined_device_data =NULL , *capacity_str =NULL ;bool noio = true;int len; +struct dm_ima_device_table_metadata *table;if (unlikely (!context))return ;wait_to_measure (&md->ima, context->update_idx); -fix_context_strings (context); -if (dm_ima_alloc_and_copy_device_data (md, &new_device_data, context, - md->ima.active_table.num_targets, - noio)) - goto error; - combined_device_data =dm_ima_alloc (DM_IMA_DEVICE_BUF_LEN *2 , noio);if (!combined_device_data) - goto error; + goto exit;if (dm_ima_alloc_and_copy_capacity_str (md, &capacity_str, noio) <0 ) - goto error; - - old_device_data = md->ima.active_table.device_metadata; - - md->ima.active_table.device_metadata = new_device_data; - md->ima.active_table.device_metadata_len =strlen (new_device_data); + goto exit; +if (md->ima.active_table.device_metadata) + old_device_data = md->ima.active_table.device_metadata; +else if (md->ima.inactive_table.device_metadata) + old_device_data = md->ima.inactive_table.device_metadata; +else + old_device_data ="device_rename=no_data;" ; +fix_context_strings (context); len =scnprintf (combined_device_data,DM_IMA_DEVICE_BUF_LEN *2 ,"%s%snew_name=%s,new_uuid=%s;%s" ,DM_IMA_VERSION_STR , old_device_data, context->dev_name, context->dev_uuid, capacity_str); -dm_ima_measure_data ("dm_device_rename" , combined_device_data, len, noio); +if (md->ima.active_table.device_metadata) { + table = &md->ima.active_table; +dm_ima_copy_device_data (md, table->device_metadata, context, + table->num_targets); + table->device_metadata_len =strlen (table->device_metadata); + } - goto exit; +if (md->ima.inactive_table.device_metadata) { + table = &md->ima.inactive_table; +dm_ima_copy_device_data (md, table->device_metadata, context, + table->num_targets); + table->device_metadata_len =strlen (table->device_metadata); + } + +dm_ima_measure_data ("dm_device_rename" , combined_device_data, len, noio); -error: -kfree (new_device_data); exit:kfree (capacity_str);kfree (combined_device_data); -kfree (old_device_data);wake_next_measure (&md->ima); }
Signature · 1 line
2.53.0
Benjamin Marzinski
maintainerPATCH v2 8/10 dm-ima: Handle race between rename and table swap
In reply to Benjamin Marzinskia device rename could happen after do_resume() removed the inactive table that it was swapping to out of the hash cell, but before it was made the active table. In this case, the table metadata would still have the old name. Update the swapped table's metadata to avoid this.
--- drivers/md/dm-ima.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/md/dm-ima.c +13 -0
@@ -373,6 +373,19 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap,kfree (md->ima.active_table.device_metadata); md->ima.active_table = context->table;memset (&context->table,0 ,sizeof (context->table)); +if (md->ima.active_table.device_metadata) { +/* +* A rename could have happened while the swap was +* going on. In that case, the saved table info would +* still have the old name. Update the metadata to be +* sure that it has the current name +*/ +struct dm_ima_device_table_metadata *table = &md->ima.active_table; +fix_context_strings (context); +dm_ima_copy_device_data (md, table->device_metadata, + context, table->num_targets); + table->device_metadata_len =strlen (table->device_metadata); + } }if (md->ima.active_table.device_metadata) {
Signature · 1 line
2.53.0
Benjamin Marzinski
maintainerPATCH v2 9/10 dm-ima: Fail more gracefully in dm_ima_measure_on_*
In reply to Benjamin MarzinskiIn all the dm_ima_measure_on_* functions besides dm_ima_measure_on_table_load(), even if measuring the event fails, it's still possible to update dm->ima, so that it continues to correctly track the device state. This means that one measurement failure won't cause future measurements to record the wrong data.
--- drivers/md/dm-ima.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
drivers/md/dm-ima.c +19 -19
@@ -357,17 +357,6 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap,wait_to_measure (&md->ima, context->update_idx); - device_table_data =dm_ima_alloc (DM_IMA_DEVICE_BUF_LEN , noio); -if (!device_table_data) - goto error; - - capacity_len =dm_ima_alloc_and_copy_capacity_str (md, &capacity_str, noio); -if (capacity_len <0 ) - goto error; - -memcpy (device_table_data + l,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR )); - l +=strlen (DM_IMA_VERSION_STR ); -if (swap) {kfree (md->ima.active_table.hash);kfree (md->ima.active_table.device_metadata);
@@ -388,6 +377,17 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap, } } + device_table_data =dm_ima_alloc (DM_IMA_DEVICE_BUF_LEN , noio); +if (!device_table_data) + goto error; + + capacity_len =dm_ima_alloc_and_copy_capacity_str (md, &capacity_str, noio); +if (capacity_len <0 ) + goto error; + +memcpy (device_table_data + l,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR )); + l +=strlen (DM_IMA_VERSION_STR ); +if (md->ima.active_table.device_metadata) {memcpy (device_table_data + l, md->ima.active_table.device_metadata, md->ima.active_table.device_metadata_len);
@@ -621,11 +621,11 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md,dm_ima_measure_data ("dm_table_clear" , device_table_data, l, noio); +error:kfree (md->ima.inactive_table.hash);kfree (md->ima.inactive_table.device_metadata);memset (&md->ima.inactive_table,0 ,sizeof (md->ima.inactive_table)); -error:kfree (capacity_str);kfree (device_table_data);
@@ -649,6 +649,8 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md,wait_to_measure (&md->ima, context->update_idx); +fix_context_strings (context); + combined_device_data =dm_ima_alloc (DM_IMA_DEVICE_BUF_LEN *2 , noio);if (!combined_device_data) goto exit;
@@ -662,11 +664,15 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md, old_device_data = md->ima.inactive_table.device_metadata; else old_device_data ="device_rename=no_data;" ; -fix_context_strings (context); len =scnprintf (combined_device_data,DM_IMA_DEVICE_BUF_LEN *2 ,"%s%snew_name=%s,new_uuid=%s;%s" ,DM_IMA_VERSION_STR , old_device_data, context->dev_name, context->dev_uuid, capacity_str); +dm_ima_measure_data ("dm_device_rename" , combined_device_data, len, noio); +exit: +kfree (capacity_str); +kfree (combined_device_data); +if (md->ima.active_table.device_metadata) { table = &md->ima.active_table; dm_ima_copy_device_data(md, table->device_metadata, context,
@@ -681,11 +687,5 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md, table->device_metadata_len =strlen (table->device_metadata); } -dm_ima_measure_data ("dm_device_rename" , combined_device_data, len, noio); - -exit: -kfree (capacity_str); -kfree (combined_device_data); -wake_next_measure (&md->ima); }
Signature · 1 line
2.53.0
Benjamin Marzinski
maintainerPATCH v2 10/10 dm-ima: use active table's size if available
In reply to Benjamin MarzinskiIt is possible that the dm_ima_measure_on_* functions run at the same time as a table is getting swapped, but before the md->ima.active_table is updated by dm_ima_measure_on_device_resume(). Instead of using the current device size, use the size of the active table that is being measured (assuming there is one), so the information is consistent. Also, don't allocate a separate string to hold the capactiy. Just print it directly to the measurement buffer.
--- drivers/md/dm-ima.c | 79 +++++++++++---------------------------------- drivers/md/dm-ima.h | 1 + 2 files changed, 20 insertions(+), 60 deletions(-)
drivers/md/dm-ima.c +19 -60
@@ -165,22 +165,10 @@ static void dm_ima_measure_data(const char *event_name, const void *buf, size_tmemalloc_noio_restore (noio_flag); } -/* -* Internal function to allocate and copy current device capacity for IMA measurements. -*/ -static int dm_ima_alloc_and_copy_capacity_str (struct mapped_device *md,char **capacity_str, -bool noio) +static sector_t dm_ima_capacity (struct mapped_device *md) { -sector_t capacity; - - capacity =get_capacity (md->disk); - - *capacity_str =dm_ima_alloc (DM_IMA_DEVICE_CAPACITY_BUF_LEN , noio); -if (!(*capacity_str)) -return -ENOMEM ; - -return scnprintf (*capacity_str,DM_IMA_DEVICE_BUF_LEN ,"current_device_capacity=%llu;" , - capacity); +return (md->ima.active_table.device_metadata) ? + md->ima.active_table.capacity :get_capacity (md->disk); }/*
@@ -320,6 +308,7 @@ void dm_ima_measure_on_table_load(struct dm_table *table, table->md->ima.inactive_table.hash = digest_buf; table->md->ima.inactive_table.hash_len =strlen (digest_buf); table->md->ima.inactive_table.num_targets = num_targets; + table->md->ima.inactive_table.capacity =dm_table_get_size (table);kfree (table->md->ima.inactive_table.device_metadata);
@@ -344,13 +333,12 @@ void dm_ima_measure_on_table_load(struct dm_table *table,void dm_ima_measure_on_device_resume (struct mapped_device *md,bool swap,struct dm_ima_context *context) { -char *device_table_data =NULL , *capacity_str =NULL ; +char *device_table_data =NULL ;char active[] ="active_table_hash=" ;unsigned int active_len =strlen (active);unsigned int l =0 ;bool noio = true;bool nodata = true; -int capacity_len;if (unlikely (!context))return ;
@@ -381,10 +369,6 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap,if (!device_table_data) goto error; - capacity_len =dm_ima_alloc_and_copy_capacity_str (md, &capacity_str, noio); -if (capacity_len <0 ) - goto error; -memcpy (device_table_data + l,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR )); l +=strlen (DM_IMA_VERSION_STR );
@@ -417,14 +401,12 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap,DM_IMA_VERSION_STR , context->dev_name, context->dev_uuid); } - -memcpy (device_table_data + l, capacity_str, capacity_len); - l += capacity_len; + l +=scnprintf (device_table_data + l,DM_IMA_DEVICE_BUF_LEN - l, +"current_device_capacity=%llu;" ,dm_ima_capacity (md));dm_ima_measure_data ("dm_device_resume" , device_table_data, l, noio); error: -kfree (capacity_str);kfree (device_table_data);wake_next_measure (&md->ima);
@@ -437,21 +419,18 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all,struct dm_ima_context *context, unsigned int idx) { - char *device_table_data, *capacity_str = NULL; + char *device_table_data;char active_table_str[] ="active_table_hash=" ;char inactive_table_str[] ="inactive_table_hash=" ;char device_active_str[] ="device_active_metadata=" ;char device_inactive_str[] ="device_inactive_metadata=" ; -char remove_all_str[] ="remove_all=" ;unsigned int active_table_len =strlen (active_table_str);unsigned int inactive_table_len =strlen (inactive_table_str);unsigned int device_active_len =strlen (device_active_str);unsigned int device_inactive_len =strlen (device_inactive_str); -unsigned int remove_all_len =strlen (remove_all_str);unsigned int l =0 ;bool noio = true;bool nodata = true; -int capacity_len;wait_to_measure (&md->ima, idx);
@@ -462,12 +441,6 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all,if (!device_table_data) goto exit; - capacity_len =dm_ima_alloc_and_copy_capacity_str (md, &capacity_str, noio); -if (capacity_len <0 ) { -kfree (device_table_data); - goto exit; - } -memcpy (device_table_data + l,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR )); l +=strlen (DM_IMA_VERSION_STR );
@@ -533,18 +506,13 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all, context->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 ; - -memcpy (device_table_data + l, capacity_str, capacity_len); - l += capacity_len; + l +=scnprintf (device_table_data + l, (DM_IMA_DEVICE_BUF_LEN *2 ) - l, +"remove_all=%c;current_device_capacity=%llu;" , + remove_all ?'y' :'n' ,dm_ima_capacity (md));dm_ima_measure_data ("dm_device_remove" , device_table_data, l, noio);kfree (device_table_data); -kfree (capacity_str); exit:kfree (md->ima.active_table.device_metadata);kfree (md->ima.inactive_table.device_metadata);
@@ -565,12 +533,11 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md, struct dm_ima_context *context) {unsigned int l =0 ; -char *device_table_data =NULL , *capacity_str =NULL ; +char *device_table_data =NULL ;char inactive_str[] ="inactive_table_hash=" ;unsigned int inactive_len =strlen (inactive_str);bool noio = true;bool nodata = true; -int capacity_len;if (unlikely (!context))return ;
@@ -581,10 +548,6 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md,if (!device_table_data) goto error; - capacity_len =dm_ima_alloc_and_copy_capacity_str (md, &capacity_str, noio); -if (capacity_len <0 ) - goto error; -memcpy (device_table_data + l,DM_IMA_VERSION_STR ,strlen (DM_IMA_VERSION_STR )); l +=strlen (DM_IMA_VERSION_STR );
@@ -616,8 +579,8 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md, context->dev_uuid); } -memcpy (device_table_data + l, capacity_str, capacity_len); - l += capacity_len; + l +=scnprintf (device_table_data + l,DM_IMA_DEVICE_BUF_LEN - l, +"current_device_capacity=%llu;" ,dm_ima_capacity (md));dm_ima_measure_data ("dm_table_clear" , device_table_data, l, noio);
@@ -626,7 +589,6 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md,kfree (md->ima.inactive_table.device_metadata);memset (&md->ima.inactive_table,0 ,sizeof (md->ima.inactive_table)); -kfree (capacity_str);kfree (device_table_data);wake_next_measure (&md->ima);
@@ -639,7 +601,7 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md, struct dm_ima_context *context) {char *old_device_data =NULL ; -char *combined_device_data =NULL , *capacity_str =NULL ; +char *combined_device_data =NULL ;bool noio = true;int len;struct dm_ima_device_table_metadata *table;
@@ -655,9 +617,6 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md,if (!combined_device_data) goto exit; -if (dm_ima_alloc_and_copy_capacity_str (md, &capacity_str, noio) <0 ) - goto exit; -if (md->ima.active_table.device_metadata) old_device_data = md->ima.active_table.device_metadata;else if (md->ima.inactive_table.device_metadata)
@@ -665,14 +624,14 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md,else old_device_data ="device_rename=no_data;" ; len =scnprintf (combined_device_data,DM_IMA_DEVICE_BUF_LEN *2 , -"%s%snew_name=%s,new_uuid=%s;%s" ,DM_IMA_VERSION_STR , old_device_data, - context->dev_name, context->dev_uuid, capacity_str); +"%s%snew_name=%s,new_uuid=%s;current_device_capacity=%llu;" , +DM_IMA_VERSION_STR , old_device_data, context->dev_name, + context->dev_uuid,dm_ima_capacity (md));dm_ima_measure_data ("dm_device_rename" , combined_device_data, len, noio); -exit: -kfree (capacity_str);kfree (combined_device_data); +exit:if (md->ima.active_table.device_metadata) { table = &md->ima.active_table; dm_ima_copy_device_data(md, table->device_metadata, context,
drivers/md/dm-ima.h +1 -0
@@ -41,6 +41,7 @@ struct dm_ima_device_table_metadata {char *device_metadata;unsigned int device_metadata_len;unsigned int num_targets; +sector_t capacity;/* * Contains the sha256 hashes of the IMA measurements of the target
Signature · 1 line
2.53.0
Applied, thanks.
Mikulas
Quoted 85 lines · Benjamin Marzinski
The dm-ima code does not guarantee that the dm_ima_measure_on_* functions will not be called at the same time. Since they modify and free shared memory, this can lead to Use-After-Free errors or garbage measurements. Further, they don't make sure that the state they measure corresponds to the actual device state. For instance if
table_load()runs at the same time asdo_resume()on a table swap,dm_ima_measure_on_device_resume()can end up thinking the wrong table is active. Or a concurrentdm_hash_rename()and a table swap, can end up with a the new active table still using the old name. This patchset makes sure the the dm-ima function are serialized and report the correct device state.However, the code is still messier that in could be. This is because it duplicates the current measurement events and format. I would really like to know if that is necessary. Specifically, it currently measures the following dm device and table actions:
load
clear
rename
resume
removeI don't see the benefit of reporting changes to the inactive table, or resumes where the device does not change state. From the user's point of view, the device is still the same after these events. At the same time, it doesn't measure device creates if no table was loaded, so you can have situations where the the first measurement for a device is a rename or a remove. A more sensible set of actions to measure would be:
create
table_swap
rename
removeAlso, the measurement format doesn't map well to how dm device's are actually set up, in a way that makes it harder for the code and records extraneous information. First, like I mentioned before, I don't see the benefit of measuring the inactive table. Second, the name, uuid, and major/minor numbers are properties of the device, not it's table (and dm devices can't have partitions, so the minor count will always be 1). I don't see a reason to store and occasinally log this information twice, if there is an active and incative table, and it forces extra coordination between the dm_ima_measure_on_* functions.
I'm wondering it we are stuck with the current events and format, now that this has been released? Or could we bump the version, and change what events we measure, and how we format the output?
Changes in v2:
- 05/10: Switched from using atomics and barriers to using a spinlock to serialize the dm_ima_measure_on_* functions
- 05/10: Fixed typos in comments and commit message
- 06/10: changedno_datastring from "table_rename" to "device_rename"If this patchset is going in largely as it is, I should point out that there are changes to the ima measurements in corner cases, where the data was previously incorrect (as mentioned in the patches). However it's possible, although I don't think very likely, that systems are expecting these incorrect measurements. We could bump the version number because of this possible change in the measurements, but IIUC, since this version number itself is part of the IMA measurements, that would guarantee that systems would get different measurements.
Benjamin Marzinski (10):
dm-ima: removedm_ima_reset_data()
dm-ima: remove brokenlast_target_measuredlogic
dm-ima: Removestatus_flagsfromdm_ima_measure_on_table_load()
dm-ima: don't copy the active table to the inactive table
dm-ima: Fix UAF errors and measuring incorrect context
dm-ima: removenew_mapfromdm_ima_measure_on_device_clear
dm-ima: Fix issues withdm_ima_measure_on_device_rename
dm-ima: Handle race between rename and table swap
dm-ima: Fail more gracefully in dm_ima_measure_on_*
dm-ima: use active table's size if availabledrivers/md/dm-ima.c | 492 +++++++++++++++++++----------------------- drivers/md/dm-ima.h | 68 ++++-- drivers/md/dm-ioctl.c | 146 +++++++++++-- drivers/md/dm.c | 2 +- 4 files changed, 410 insertions(+), 298 deletions(-)Signature · 1 line
2.53.0