[image] Make image data read-only to most consumers

Almost all image consumers do not need to modify the content of the
image.  Now that the image data is a pointer type (rather than the
opaque userptr_t type), we can rely on the compiler to enforce this at
build time.

Change the .data field to be a const pointer, so that the compiler can
verify that image consumers do not modify the image content.  Provide
a transparent .rwdata field for consumers who have a legitimate (and
now explicit) reason to modify the image content.

We do not attempt to impose any runtime restriction on checking
whether or not an image is writable.  The only existing instances of
genuinely read-only images are the various unit test images, and it is
acceptable for defective test cases to result in a segfault rather
than a runtime error.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-04-30 14:14:51 +01:00
parent cd803ff2e2
commit 05ad7833c5
15 changed files with 45 additions and 73 deletions

View File

@@ -1079,7 +1079,7 @@ int cms_decrypt ( struct cms_message *cms, struct image *image,
final_len = ( ( image->len && is_block_cipher ( cipher ) ) ?
cipher->blocksize : 0 );
bulk_len = ( image->len - final_len );
cipher_decrypt ( cipher, ctx, image->data, image->data, bulk_len );
cipher_decrypt ( cipher, ctx, image->data, image->rwdata, bulk_len );
/* Decrypt final block */
cipher_decrypt ( cipher, ctx, ( image->data + bulk_len ), final,
@@ -1117,7 +1117,7 @@ int cms_decrypt ( struct cms_message *cms, struct image *image,
* have to include include any error-handling code path to
* reconstruct the block padding.
*/
memcpy ( ( image->data + bulk_len ), final, final_len );
memcpy ( ( image->rwdata + bulk_len ), final, final_len );
image->len -= pad_len;
/* Clear image type and re-register image, if applicable */
@@ -1137,7 +1137,7 @@ int cms_decrypt ( struct cms_message *cms, struct image *image,
* containing the potentially invalid (and therefore
* unreproducible) block padding.
*/
cipher_encrypt ( cipher, ctxdup, image->data, image->data, bulk_len );
cipher_encrypt ( cipher, ctxdup, image->data, image->rwdata, bulk_len );
if ( original_flags & IMAGE_REGISTERED ) {
register_image ( image ); /* Cannot fail on re-registration */
image_put ( image );