genprotimg: make sure the kernel command line is always null-terminated

Make sure that the kernel command line used for the Secure Execution
boot image is always null-terminated. Before this change, users had to
ensure that the provided kernel cmdline was null-terminated, which was
error-prone. But since the default s390x Linux kernel command line is
set to `root=/dev/ram0 ro` the remaining reserved memory for the kernel
command line is zeroed out. Therefore, the problem only shows up if the
used kernel command line is shorter than the default kernel command
line.

Fixes: 65b9fc442c ("genprotimg: introduce new tool for the creation of PV images")
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
This commit is contained in:
Marc Hartmayer
2023-10-20 13:44:29 +00:00
committed by Steffen Eiden
parent 71b93d55ef
commit 5a7d7e05b8
3 changed files with 33 additions and 1 deletions

View File

@@ -684,7 +684,26 @@ gint pv_img_add_component(PvImage *img, const PvArg *arg, GError **err)
{
g_autoptr(PvComponent) comp = NULL;
comp = pv_component_new_file(arg->type, arg->path, err);
switch (arg->type) {
case PV_COMP_TYPE_INITRD:
case PV_COMP_TYPE_KERNEL:
case PV_COMP_TYPE_STAGE3B:
comp = pv_component_new_file(arg->type, arg->path, err);
break;
case PV_COMP_TYPE_CMDLINE: {
g_autoptr(PvBuffer) buf = NULL;
g_autofree char *data = NULL;
gsize length;
if (!g_file_get_contents(arg->path, &data, &length, err))
return -1;
/* Add one for the null terminator */
buf = pv_buffer_take(g_steal_pointer(&data), length + 1);
comp = pv_component_new_buf(arg->type, buf, err);
} break;
}
if (!comp)
return -1;

View File

@@ -26,6 +26,15 @@ PvBuffer *pv_buffer_alloc(gsize size)
return ret;
}
PvBuffer *pv_buffer_take(char *data, gsize size)
{
PvBuffer *ret = g_new0(PvBuffer, 1);
ret->data = data;
ret->size = size;
return ret;
}
PvBuffer *pv_buffer_dup(const PvBuffer *buf, gboolean page_aligned)
{
PvBuffer *ret;

View File

@@ -21,6 +21,10 @@ typedef struct PvBuffer {
} PvBuffer;
PvBuffer *pv_buffer_alloc(gsize size);
/* After this call @data belongs to the PvBuffer and must no longer be modified
* by the caller.
*/
PvBuffer *pv_buffer_take(char *data, gsize size);
void pv_buffer_free(PvBuffer *buf);
void pv_buffer_clear(PvBuffer **buf);
gint pv_buffer_write(const PvBuffer *buf, FILE *file, GError **err);