system: reject sysupgrade of broken firmware images
authorRafał Miłecki <rafal@milecki.pl>
Fri, 30 Aug 2019 15:46:07 +0000 (17:46 +0200)
committerRafał Miłecki <rafal@milecki.pl>
Wed, 4 Sep 2019 04:21:18 +0000 (06:21 +0200)
This uses recently added "validate_firmware_image" to validate passed
firmware. If it happens to be invalid and marked as impossible to force
then sysupgrade simply exits with an error.

This change is needed to avoid bricking devices with some totally broken
images.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
system.c

index 35d5a233e4a303360d32be12a92b3409c6967746..7d7073d00a06418da9e3090c318a82b90112e511 100644 (file)
--- a/system.c
+++ b/system.c
@@ -507,7 +507,18 @@ static int sysupgrade(struct ubus_context *ctx, struct ubus_object *obj,
                      struct ubus_request_data *req, const char *method,
                      struct blob_attr *msg)
 {
+       enum {
+               VALIDATION_VALID,
+               VALIDATION_FORCEABLE,
+               __VALIDATION_MAX
+       };
+       static const struct blobmsg_policy validation_policy[__VALIDATION_MAX] = {
+               [VALIDATION_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_BOOL },
+               [VALIDATION_FORCEABLE] = { .name = "forceable", .type = BLOBMSG_TYPE_BOOL },
+       };
+       struct blob_attr *validation[__VALIDATION_MAX];
        struct blob_attr *tb[__SYSUPGRADE_MAX];
+       bool valid, forceable;
 
        if (!msg)
                return UBUS_STATUS_INVALID_ARGUMENT;
@@ -516,6 +527,21 @@ static int sysupgrade(struct ubus_context *ctx, struct ubus_object *obj,
        if (!tb[SYSUPGRADE_PATH] || !tb[SYSUPGRADE_PREFIX])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
+       if (validate_firmware_image_call(blobmsg_get_string(tb[SYSUPGRADE_PATH])))
+               return UBUS_STATUS_UNKNOWN_ERROR;
+
+       blobmsg_parse(validation_policy, __VALIDATION_MAX, validation, blob_data(b.head), blob_len(b.head));
+
+       valid = validation[VALIDATION_VALID] && blobmsg_get_bool(validation[VALIDATION_VALID]);
+       forceable = validation[VALIDATION_FORCEABLE] && blobmsg_get_bool(validation[VALIDATION_FORCEABLE]);
+
+       if (!valid) {
+               if (!forceable) {
+                       fprintf(stderr, "Firmware image is broken and cannot be installed\n");
+                       return UBUS_STATUS_NOT_SUPPORTED;
+               }
+       }
+
        sysupgrade_exec_upgraded(blobmsg_get_string(tb[SYSUPGRADE_PREFIX]),
                                 blobmsg_get_string(tb[SYSUPGRADE_PATH]),
                                 tb[SYSUPGRADE_COMMAND] ? blobmsg_get_string(tb[SYSUPGRADE_COMMAND]) : NULL,