Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2806851pxu; Mon, 14 Dec 2020 11:18:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJwmLXzzucnLjBUcY3KnQxKm3XhePqMRaxBBR3twsIjLK9gAA4IBfj2iOvWJhFDhdDZSXESe X-Received: by 2002:a05:6402:2377:: with SMTP id a23mr11194388eda.34.1607973529419; Mon, 14 Dec 2020 11:18:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607973529; cv=none; d=google.com; s=arc-20160816; b=aTepmDQj4/nyhaDXd3j7v5rwtn0sw7NVzPkZ5RZukz7T+KPQSOoxkgHp5VXAs+iNm2 X96Aadv2oor6z8AoNwuEO40hIU7Af7Tact2XkrNCwWAoC+PV4bq4Ge2jRsIwQb//22Or 95+e9F4wL7/MizjICzbf7/VOIIaRrn+U5r02gzi7ZKc8ZDnoIEs8rluHaErc8wmrT5Wz 1SxAg5GS549f7O45mnydrXbvfqXlgg5b0qMzHmnpZvzDM+kqbZLiCVsUGgSc1tjuIkMG 1fKLX6qe9nhE/mB85pBtJZzpor2mX3SZVp45+8oaLVyo80wOMJH+twsziCfgPj1/kxz8 y/xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from; bh=skUjSXeU45LmL0Wr4AAG03tgHjEFwG3/X7wgNgRz+FE=; b=spZqQzc1sBpGmk152grKmOIJiKH7ibxE0RhYbzo4iINxP2LiiDvPNBdHqWm7zCXaV5 CN1P6Pml4rPykrKLU7YsJCjXYsuTVejG7YtlQ+E6EcUQbQtit5fEKavji7Vq8KLvCJq1 /pWMf8KHrlQ/o//WkMKI/w9qmRhReFHzwzULYGIqHUk6eRUS3AloYHPRbGLwvXyyn3Wx 1qaRg7S7fEJSYkSfdHDnVvu2CW+3v65IejmRptRoCvG7Mc62jqJYfSdqhfcRdHihX9YF 4Fq8nIs+9dvrwTm8aqzJDaECDcElpNxdJt37qnNWKBxl1V6JVGQTv1wLD3/XLMFOqEfo RSRw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e4si10362167ejq.356.2020.12.14.11.18.25; Mon, 14 Dec 2020 11:18:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2408932AbgLNTI6 (ORCPT + 99 others); Mon, 14 Dec 2020 14:08:58 -0500 Received: from mail.kernel.org ([198.145.29.99]:46086 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2408581AbgLNRhK (ORCPT ); Mon, 14 Dec 2020 12:37:10 -0500 From: Greg Kroah-Hartman Authentication-Results: mail.kernel.org; dkim=permerror (bad message/signature format) To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Linus Torvalds , Masami Hiramatsu , "Steven Rostedt (VMware)" , Sasha Levin Subject: [PATCH 5.9 006/105] tools/bootconfig: Fix to check the write failure correctly Date: Mon, 14 Dec 2020 18:27:40 +0100 Message-Id: <20201214172555.591383855@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201214172555.280929671@linuxfoundation.org> References: <20201214172555.280929671@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masami Hiramatsu [ Upstream commit a995e6bc0524450adfd6181dfdcd9d0520cfaba5 ] Fix to check the write(2) failure including partial write correctly and try to rollback the partial write, because if there is no BOOTCONFIG_MAGIC string, we can not remove it. Link: https://lkml.kernel.org/r/160576521135.320071.3883101436675969998.stgit@devnote2 Fixes: 85c46b78da58 ("bootconfig: Add bootconfig magic word for indicating bootconfig explicitly") Suggested-by: Linus Torvalds Signed-off-by: Masami Hiramatsu Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Sasha Levin --- tools/bootconfig/main.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c index e0878f5f74b1b..ffd6a358925da 100644 --- a/tools/bootconfig/main.c +++ b/tools/bootconfig/main.c @@ -274,6 +274,7 @@ static void show_xbc_error(const char *data, const char *msg, int pos) int apply_xbc(const char *path, const char *xbc_path) { + struct stat stat; u32 size, csum; char *buf, *data; int ret, fd; @@ -330,16 +331,26 @@ int apply_xbc(const char *path, const char *xbc_path) return fd; } /* TODO: Ensure the @path is initramfs/initrd image */ + if (fstat(fd, &stat) < 0) { + pr_err("Failed to get the size of %s\n", path); + goto out; + } ret = write(fd, data, size + 8); - if (ret < 0) { + if (ret < size + 8) { + if (ret < 0) + ret = -errno; pr_err("Failed to apply a boot config: %d\n", ret); - goto out; + if (ret < 0) + goto out; + goto out_rollback; } /* Write a magic word of the bootconfig */ ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN); - if (ret < 0) { + if (ret < BOOTCONFIG_MAGIC_LEN) { + if (ret < 0) + ret = -errno; pr_err("Failed to apply a boot config magic: %d\n", ret); - goto out; + goto out_rollback; } ret = 0; out: @@ -347,6 +358,17 @@ out: free(data); return ret; + +out_rollback: + /* Map the partial write to -ENOSPC */ + if (ret >= 0) + ret = -ENOSPC; + if (ftruncate(fd, stat.st_size) < 0) { + ret = -errno; + pr_err("Failed to rollback the write error: %d\n", ret); + pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path); + } + goto out; } int usage(void) -- 2.27.0