From patchwork Thu Aug 20 08:28:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "S. Lockwood-Childs" X-Patchwork-Id: 1348238 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::63d; helo=mail-pl1-x63d.google.com; envelope-from=swupdate+bncbdgpj5oqwyebb47g7d4qkgqes7xydgy@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=vctlabs.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=kEz+ls0M; dkim-atps=neutral Received: from mail-pl1-x63d.google.com (mail-pl1-x63d.google.com [IPv6:2607:f8b0:4864:20::63d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BXHlZ1CJlz9sR4 for ; Thu, 20 Aug 2020 18:25:28 +1000 (AEST) Received: by mail-pl1-x63d.google.com with SMTP id g17sf1121902plg.10 for ; Thu, 20 Aug 2020 01:25:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1597911924; cv=pass; d=google.com; s=arc-20160816; b=mlmBNLGAHex7UF+r/P9f0dHmIp0IELTC1fdPhq6tjah/tfVYD/GShiRog1DXWLA9us /qHXXCWCmP+Pri6NiqAqL520WhqQ8R+5eN1q8f1ZRx4SmXem3YEgSJFjfff960K0KW39 fogIW938csIM3Hhi8/tHcKNNSRk6p22HM+X474vSZZOf6xSWl+y90L1uTcDXFf+SFWJh xg5KD8OQWoyb/mLHtlReEMuI6Emvw+eg1o4lEUcLHdXJwiq92nQH9M2xDFU6BJptg3z0 NLGkmeGQgmRc4C9LOdnoTjq9NdmD3AgZcmoNI9R3DvYofLx7XudobbNwdHDYIJM9gfSV ZKcw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:user-agent:content-disposition :mime-version:message-id:subject:to:from:date:sender:dkim-signature; bh=Z1zHcy1B46uOOkVf0+Tde47a2SXrC/EFiREZBnNuTEU=; b=QV2PudER8YwLTjmNaq+kCu9xhZ9mTj7/bgv7u5Z1w/ovFmcnmowl972I831lboCkyy stVGkrWtC3L0ax9fATHkyA91KbEUFTAww25xwkHGUqbZZ7IG/8xFJ6WAaOL7fo4qnIEp 9ovDczzJhtKt//swrwRAsTVEukBjSXSyNXzAE7PMqpjtUw4SZlhnluXhwB4jN1C/1jHx 4iCgw280VfxfmKlJKpiAmHI9U1atxcbTVimAsQNlXPmCeNtRSjwKnWRLQdbkcuqmekTW zTnuwh4eeO7wJj9Rft3o0qhkCYn5K8myAtWy67H4QmKnZvNbTTgsFbUkNt5LeV0acZFX dl6Q== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 207.154.65.135 is neither permitted nor denied by best guess record for domain of sjl@vctlabs.com) smtp.mailfrom=sjl@vctlabs.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:date:from:to:subject:message-id:mime-version :content-disposition:user-agent:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-subscribe:list-unsubscribe; bh=Z1zHcy1B46uOOkVf0+Tde47a2SXrC/EFiREZBnNuTEU=; b=kEz+ls0Mqfl0cqUVivRQF1IC2zRi7eeu8LomjGfy7BmMzoQx5yosmmSBqVVUWiya+Q JYSCQx5ltYwtQsq3vVyG/USy3pjzzW9/LKm1XRDXrd5Qys6oiXH7JnhTZWjuQMhcl9XT 6WsSJylIecWVRNWinvrGaFGAa/zlFEIcMe18tpqah5ydME2x0BjCXVrqqe6bhKDif9U7 vao1fsbDMs6qk3KRAwLgMEQ5lHnr0S2sT5i0LcWRtlidZ10nUkE5JlSAvlpAlQXlcvE6 SFpvKEuM5l/QP6/GMFr7LUt5q6WkknpJ9lxiHne/+SY543d0HnozVV2J8GxHCLGAGizq WX3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:date:from:to:subject:message-id :mime-version:content-disposition:user-agent:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=Z1zHcy1B46uOOkVf0+Tde47a2SXrC/EFiREZBnNuTEU=; b=l9qAZuOz2ykfIKeVUb8AblzyzcAIW6aRxQhlLFaXept884uTAKbWMUmS04GSoDml0j p3zpN6U+kG+z8YaCIy56Sfe391xBChm9HcXJ5r+br86LGT4+j1z24+OF2Elp1XeLE2QW SJaESVAyZ+8ACC6u40U2rfQZnSxC4RGVFWxdRKGwxvTprAfSt7z1TLMQUDavCvy/hJYJ lV9tQIOTuIuDylVt257+jetFGG15+UKn6v0ZkDZOtaKXS6CKTU0mP/x53S92DXFEuUJO CsUJS0mnioBd7XuHoj5gqpI/wSly3Mtkc/Gy/DHw/6wkGMd3L62EHdC1p5oN61CvN0iS gPwg== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM533O0mwS9V+zz0XY3TKLjXMVlwG9ZbJDI+Xl6nYzeNdnwaSa3dB3 ddynxaP7QjE31UadkKsGH5g= X-Google-Smtp-Source: ABdhPJzXJ29UoH8YuHSZtAMII3fHbK78mlBYm61Inzluuu6OghFtfmiWY0jKhDlrCuuFmkZ9uwNtbQ== X-Received: by 2002:aa7:959c:: with SMTP id z28mr1419082pfj.223.1597911924132; Thu, 20 Aug 2020 01:25:24 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a65:620d:: with SMTP id d13ls458696pgv.0.gmail; Thu, 20 Aug 2020 01:25:23 -0700 (PDT) X-Received: by 2002:aa7:9519:: with SMTP id b25mr1409923pfp.292.1597911923283; Thu, 20 Aug 2020 01:25:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597911923; cv=none; d=google.com; s=arc-20160816; b=VsR583jd9V0WYtRc7vkGkpeWWQcG3gksJkLVwjq0yHI4YrRfcc/uJvhXZvIJ9THMtC 2d75OGe51bLYibRcMtP/D5qHWwLtWaZfSBrGDVCsG8c0IBOLduQjqp1HnTR/sulAPKfZ jkZpsHfZbqhhOTwr75gTGtRnJp9ahmhvLG5cka3h0q3ksAzTmp55qgAXd/QyYcRCAVm7 gOD+7Ny/jhS6N5C2uutLCqlobpvyWSHvMluwHmrQMLOHT+SYkLgnVsd802VM/FTgxb8X DCA+wZ14+vJl0G88L9typWcF1XtjDj9bk8lnhnLtGmQtcVCgOmvGBFRJ3c48HUJmFj1P XoVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:content-disposition:mime-version:message-id:subject:to :from:date; bh=/XRFuU1yKvsq/Q0ZCYWe0bdlX0SSyTciOOuzNQTWK4g=; b=tAkdZ1WFnqrNfcMEQI5XIRDoyHJ3FQPyY8aPkdYAlJeOocuUwqiTffS4xp0Q7vA9QC OgQqnqnP6uK/Yne/CXJvJh49hyRNJxjID5zPmVtCm0GgvbXBQI8TL71ZGHXXTI4Yw3Lk aATf2hEbcRetiqVheiRtcGmFKDCx5oBSbP92xrDIFNfhYyfafR1K8dsYDtxjSBC0Cram kptC5eKiJMKID+DovzQzV0yC9PdwbWm5YK1cI5vN2DhPRrH0KnCNiPzHp+QT2RHYKmEE acxY//vUKlXf+P0iSuOdmraIqe9Ukt+siB1tCQ3qw6O3vOJg0ApY3TgB9iu5sfs/TsXP muPw== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 207.154.65.135 is neither permitted nor denied by best guess record for domain of sjl@vctlabs.com) smtp.mailfrom=sjl@vctlabs.com Received: from dent.vctlabs.com (net-cf9a4187.iis.impulse.net. [207.154.65.135]) by gmr-mx.google.com with ESMTPS id 91si55558pjz.1.2020.08.20.01.25.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 20 Aug 2020 01:25:23 -0700 (PDT) Received-SPF: neutral (google.com: 207.154.65.135 is neither permitted nor denied by best guess record for domain of sjl@vctlabs.com) client-ip=207.154.65.135; Received: by dent.vctlabs.com (Postfix, from userid 1000) id 98E6E2803AB; Thu, 20 Aug 2020 01:28:43 -0700 (PDT) Date: Thu, 20 Aug 2020 01:28:43 -0700 From: "S. Lockwood-Childs" To: swupdate@googlegroups.com Subject: [swupdate] [PATCH] Fix regression in UBI volume support Message-ID: <20200820082843.s2jwxcqq53xtfon3@vctlabs.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20170113 (1.7.2) X-Original-Sender: sjl@vctlabs.com X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 207.154.65.135 is neither permitted nor denied by best guess record for domain of sjl@vctlabs.com) smtp.mailfrom=sjl@vctlabs.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Running realpath() directly on the name parsed from the env config file fails in the special case of UBI volume syntax, e.g. /dev/ubi0:env ^^^------ UBI volume name That special case with UBI volume name is handled by ubi_update_name(), which doesn't happen early enough for normalizing the device path. With above example, libuboot_read_config() would attempt to normalize "/dev/ubi0:env" which does not exist. Separate out the path normalization into a helper function that splits off any volume part (e.g. ":env") before trying to normalize the path, then appends the volume part to the normalized path. Signed-off-by: S. Lockwood-Childs Acked-by: Stefano Babic --- src/libuboot.h | 2 ++ src/uboot_env.c | 57 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/libuboot.h b/src/libuboot.h index 06a2a3f..bfcaeb1 100644 --- a/src/libuboot.h +++ b/src/libuboot.h @@ -15,6 +15,8 @@ struct uboot_ctx; #define DEVNAME_MAX_LENGTH 256 +#define DEVNAME_SEPARATOR ':' + /** Configuration passed in initialization * */ diff --git a/src/uboot_env.c b/src/uboot_env.c index 7aae6aa..3a51d26 100644 --- a/src/uboot_env.c +++ b/src/uboot_env.c @@ -268,7 +268,7 @@ static int ubi_update_name(struct uboot_flash_env *dev) int dev_id, vol_id, ret = -EBADF; char *sep; - sep = index(dev->devname, ':'); + sep = index(dev->devname, DEVNAME_SEPARATOR); if (sep) { memset(device, 0, DEVNAME_MAX_LENGTH); @@ -294,6 +294,54 @@ out: return ret; } +static int normalize_device_path(char *path, struct uboot_flash_env *dev) +{ + char *sep = NULL, *normalized = NULL; + size_t normalized_len = 0, volume_len = 0, output_len = 0; + + /* + * if volume name is present, split into device path and volume + * since only the device path needs normalized + */ + sep = index(path, DEVNAME_SEPARATOR); + if (sep) + { + volume_len = strlen(sep); + *sep = '\0'; + } + + if ((normalized = realpath(path, NULL)) == NULL) + { + /* device file didn't exist */ + return -EINVAL; + } + + normalized_len = strlen(normalized); + output_len = sizeof(dev->devname) - 1; /* leave room for null */ + if ((normalized_len + volume_len) > output_len) + { + /* full name is too long to fit */ + free(normalized); + return -EINVAL; + } + + /* + * save normalized path to device file, + * and possibly append separator char & volume name + */ + memset(dev->devname, 0, sizeof(dev->devname)); + strncpy(dev->devname, normalized, output_len); + free(normalized); + + if (sep) + { + *sep = DEVNAME_SEPARATOR; + strncpy(dev->devname + normalized_len, sep, output_len - normalized_len); + } + + return 0; +} + static int check_env_device(struct uboot_ctx *ctx, struct uboot_flash_env *dev) { int fd, ret; @@ -1115,7 +1163,6 @@ int libuboot_read_config(struct uboot_ctx *ctx, const char *config) int ndev = 0; struct uboot_flash_env *dev; char *tmp; - char *path; int retval = 0; if (!config) @@ -1158,16 +1205,12 @@ int libuboot_read_config(struct uboot_ctx *ctx, const char *config) ctx->size = dev->envsize; if (tmp) { - if ((path = realpath(tmp, NULL)) == NULL) { + if (normalize_device_path(tmp, dev) < 0) { free(tmp); retval = -EINVAL; break; } - - memset(dev->devname, 0, sizeof(dev->devname)); - strncpy(dev->devname, path, sizeof(dev->devname) - 1); free(tmp); - free(path); } if (check_env_device(ctx, dev) < 0) {