From patchwork Wed Sep 13 03:45:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Hilliard X-Patchwork-Id: 1833356 X-Patchwork-Delegate: sbabic@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20230601 header.b=IxiqZNEJ; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=YIgQmNfD; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::f39; helo=mail-qv1-xf39.google.com; envelope-from=swupdate+bncbcl4hcw73qcbb3xaqsuamgqefsinzra@googlegroups.com; receiver=patchwork.ozlabs.org) Received: from mail-qv1-xf39.google.com (mail-qv1-xf39.google.com [IPv6:2607:f8b0:4864:20::f39]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RlmZW04y7z1yhL for ; Wed, 13 Sep 2023 13:45:53 +1000 (AEST) Received: by mail-qv1-xf39.google.com with SMTP id 6a1803df08f44-649fac91500sf3896036d6.1 for ; Tue, 12 Sep 2023 20:45:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1694576750; cv=pass; d=google.com; s=arc-20160816; b=yUveAra/496e01+BDNqmtyi82IMmICAmAlKceySUoi/IBJ+iTdsRJf7ZNrTe/90/hC U8ZBX1tRv3rA4aK2M5yxTxwOHJ97LgZxDgaGdQz8hxsVStjMIDsxQk1BQPpfTDzRN4Cx etYW1PvRVhChhbQyavhNeTeOnpGoLwTQWEfxnVWkNX7UXRee1LYukzX9OcaVu65/UDgS qNQI61VHx+1JuO6BsJ9HPEYKjPkKwJwa7IYHwBG55RqrsLCxu516wfWamOMZINhCNWNe k98aa9T5rHQaCQOjiPmBWTUUKpKDunGcYZT5Fy2RUm3RxIQ3rMrnXOSOiREf32N4OlsF sd1w== 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:mime-version:message-id:date :subject:cc:to:from:sender:dkim-signature:dkim-signature; bh=znNxOCtRhHyoXX5Av6Jh19Ef4nP+JgFGJHb1MmOqOIA=; fh=KPrAw848PoyEggiIX82ubXk7sJG2UD396EeWf1FQVUo=; b=vCd3CLMtCYhXfV0yP/kn+S/uU6FnCmS/ecovC4NUI9IetqAdhtAcsZQsjiukLVvzxb 5n6gKGROh+Qtk4WhkYMHlkO1sO+e+mvoRLbVbiB1+iysSgKi4NMZOZV9qrxI7rC8kwkC Uv1bn1/3aQ02Y5mR25dAEm+im0lF5dzMOklAckwJGtst06wUC+lVbwl3kJCIL7kJein4 /WpPofGRdLxdSssMSk4tlHofS8Ci8PA6e14IUkj39rpqUUNG/4jk711bbte43NDwBNLI u0iHC0sFeukV+EO2uXb+S4J/1cFnjsvH02AIPd9tiXnB09zXZAyEnxwe3hLLBO3Pcd1n Xzsg== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=m1dmt+sz; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::12b as permitted sender) smtp.mailfrom=james.hilliard1@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20230601; t=1694576750; x=1695181550; darn=patchwork.ozlabs.org; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:message-id:date:subject:cc:to:from :sender:from:to:cc:subject:date:message-id:reply-to; bh=znNxOCtRhHyoXX5Av6Jh19Ef4nP+JgFGJHb1MmOqOIA=; b=IxiqZNEJJcqUqpZfO+wLw6GXQZ8iijBqOmIAvpaeBuxBf6e+HbgFJ1crfrF9MUj9ru LMAizTwl1Ud2kgsPTbK4TrNnbYF+5yiy1IDxtT4ZPPfATke8r07zWDyOQXi5rsBjNBRa GanNVTOmOQiiQ/PAHGjXeWbhPEkBpc2vFkZXl0D0xhSDc22GdGXboAQsN778rjIeee1I SGNZbFjGoaLsx/DpN5HxE6dn7AtqTrPXYnsYO+BnOl8aPyROgIfUc+E7H0oURKXi4xga nNkSpZih1w+TmCqKEmTbpeG3Y2xbVX0i4KnBqrleskodO/Epa+qLr+Wb1tm3hmvVbIVt G15w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694576750; x=1695181550; darn=patchwork.ozlabs.org; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:message-id:date:subject:cc:to:from :from:to:cc:subject:date:message-id:reply-to; bh=znNxOCtRhHyoXX5Av6Jh19Ef4nP+JgFGJHb1MmOqOIA=; b=YIgQmNfDiJKHfe5MuAjWLqPT5pOMQMpX3WyY7ApoyZe8V2QYFJmHhx07koY4WcyeLD 2lK8ABF7UUHiPlEudqSZDFAhH3Gnhud8fiUhAjHTMXOWIsrjfTeoWBr1g9mJoCqCmnq7 i+elbWSfCn+HedVDmAbBEgB6XmIBm12ISvLbS88eQ8z8RsnimrjY2yQEJVE5X6XviiBm Jh1psHd/LtIq38H0QjpJxharVIRDykmqutZyfAh5RKxRdzl59N3uxWXxpkhwW4VtiYxd jsPwbZ0IW6aZGsjXkYgL4+Zx9QHTx6JQkUTuKJYrq24KsRYNaQi/apPmRXH0CAh+BY9w o/hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694576750; x=1695181550; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :message-id:date:subject:cc:to:from:x-beenthere:x-gm-message-state :sender:from:to:cc:subject:date:message-id:reply-to; bh=znNxOCtRhHyoXX5Av6Jh19Ef4nP+JgFGJHb1MmOqOIA=; b=RgLcedUSVhOMarhtHOpMBFGF8MkTIT9cPO9xE/xpwR6LpQAlTF28A+F4Xp37yFlYWW Z3Q4QirVAh1DWxfnY/bS6/6Y5zydLtPpUaZM5a0QFywPEcZy1EoTzi43pCP1UwWWlDCW sOTboCMT7v/S9mdsEjImn3bs+6IWv67raJyUFTnxxSh5f3IzXfYMRO0nFYM5NJgBhILM OXgOemqeVL1IglKyxgC/82/D+SMt7epDaWJmorZmCqbU0/bbDuQfYhplrmqW8QUhAk3V TZ5Xnd6BcGB8NNUMr8W3B5W1UKfLJ+who6K1gHgN60tyYppTmOowgz2tjxLrWnmMts7E wVQg== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOJu0Yx3WoiOjcvhxy8jbw1YNcIlVlhmb/+ZY9Oe+5Nzzs/GIVW9JzYF 9PbY/zN/6emfDQxuR5J16Gw= X-Google-Smtp-Source: AGHT+IEnDc4GKM72jR4/hph4NIqRHuBt/ZI3sAgtp0yyOjw5QDNQujG351M/Sb9QuM1YxfmKDBdboA== X-Received: by 2002:a0c:f286:0:b0:651:95e4:ec85 with SMTP id k6-20020a0cf286000000b0065195e4ec85mr1567834qvl.32.1694576750526; Tue, 12 Sep 2023 20:45:50 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a0c:f0d1:0:b0:635:a666:7535 with SMTP id d17-20020a0cf0d1000000b00635a6667535ls858091qvl.2.-pod-prod-00-us; Tue, 12 Sep 2023 20:45:49 -0700 (PDT) X-Received: by 2002:a67:cd8e:0:b0:447:9339:a5f with SMTP id r14-20020a67cd8e000000b0044793390a5fmr819130vsl.0.1694576749280; Tue, 12 Sep 2023 20:45:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694576749; cv=none; d=google.com; s=arc-20160816; b=pzS6U3pqcnpYi0iXOI+BSHxr+Z05dm7ndllxy8El2VDpu4oNKqPK1f4kDEL3fVwlAr gQRh9FBO5aNCnEiGaayyPbCbJUu2osTfmy3h06FvMyyJqe78XNFI6jMJx1pzNji5mti6 UISU2DuCw62H+9QKO4SYVHBRsMndxwbHx7sPz0o6dn2KVSn/w0qSSa/m/0pOofcg3X5o krglOuq49JBz9lT7Sci5f8sqBM5mSaorNq1KJWPqytsvQEsOaBcOHgJDxkjMj2TlBj4r nfUAD/C8B+JJu9VhTrjJ5k8JX3M9WMY2WZioBEkR+03lBRRLB5DTtSiOtoNLI6AQ74OB FHZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=Rq3vJmvhaZ3twaZycsk9JsS2b+h9QmwHR0uqQ5IER0I=; fh=KPrAw848PoyEggiIX82ubXk7sJG2UD396EeWf1FQVUo=; b=i4zUXlBWQJ0slVaOr2PSfX17UqU8q9WXH08j/7Av/z9tORDp97n+T4O4y2xj+aNRe7 W9AJGP/Ynid0J6cVW5FvsWbjg73IhT/awe73+Aq+uNnnZnJMb7AZAY4A6ZHUdBoPK6+p PncY0rJnoeYwnLNtuI5n8zi+2PHoKwRwBXxa6F9QpLve/DmG6uM9vJlKsFy02u9ZYcYi fcm537qmY86OZuvAjK/BHHk3ZXVkXPBolvo9V1qzuL42PmBv9M7JQ5ydmB1Zv0hfVdwP EvmX2jJSLQ6AvqY9y4gUYKuBrEcXKmgAk8diYAmYIYbPSGp1U3Ok4Len+vbweQ6dbd13 l9+Q== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=m1dmt+sz; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::12b as permitted sender) smtp.mailfrom=james.hilliard1@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-il1-x12b.google.com (mail-il1-x12b.google.com. [2607:f8b0:4864:20::12b]) by gmr-mx.google.com with ESMTPS id m13-20020a05610206cd00b0044d3c680b96si1422816vsg.2.2023.09.12.20.45.49 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 12 Sep 2023 20:45:49 -0700 (PDT) Received-SPF: pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::12b as permitted sender) client-ip=2607:f8b0:4864:20::12b; Received: by mail-il1-x12b.google.com with SMTP id e9e14a558f8ab-34ccedcc584so1426485ab.0 for ; Tue, 12 Sep 2023 20:45:49 -0700 (PDT) X-Received: by 2002:a05:6e02:1a29:b0:34f:89bf:98f5 with SMTP id g9-20020a056e021a2900b0034f89bf98f5mr4567294ile.3.1694576748125; Tue, 12 Sep 2023 20:45:48 -0700 (PDT) Received: from james-x399.localdomain (71-33-152-19.hlrn.qwest.net. [71.33.152.19]) by smtp.gmail.com with ESMTPSA id ge12-20020a056638680c00b0042b4b1246cbsm3207059jab.148.2023.09.12.20.45.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 20:45:47 -0700 (PDT) From: James Hilliard To: swupdate@googlegroups.com Cc: James Hilliard Subject: [swupdate] [PATCH 1/1] lua: prevent copy2file from corrupting input file Date: Tue, 12 Sep 2023 21:45:23 -0600 Message-Id: <20230913034523.177625-1-james.hilliard1@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Original-Sender: james.hilliard1@gmail.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=m1dmt+sz; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::12b as permitted sender) smtp.mailfrom=james.hilliard1@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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: , If we call copy2file with the temporary filepath of our input file it will result in hash mismatch errors that can be tricky to identify properly. It can also result in a corrupt output file. To prevent this ensure that fdin and fdout point to different files. Signed-off-by: James Hilliard Reviewed-by: Stefano Babic --- core/util.c | 7 +++++++ corelib/lua_interface.c | 6 ++++++ include/util.h | 1 + 3 files changed, 14 insertions(+) diff --git a/core/util.c b/core/util.c index a6ddf82..9ab0a17 100644 --- a/core/util.c +++ b/core/util.c @@ -1211,3 +1211,10 @@ bool img_check_free_space(struct img_type *img, int fd) return check_free_space(fd, size, img->fname); } + +bool check_same_file(int fd1, int fd2) { + struct stat stat1, stat2; + if(fstat(fd1, &stat1) < 0) return false; + if(fstat(fd2, &stat2) < 0) return false; + return (stat1.st_dev == stat2.st_dev) && (stat1.st_ino == stat2.st_ino); +} diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c index dd925b7..b68cf32 100644 --- a/corelib/lua_interface.c +++ b/corelib/lua_interface.c @@ -393,6 +393,12 @@ static int l_copy2file(lua_State *L) uint32_t checksum = 0; table2image(L, &img); + if (check_same_file(img.fdin, fdout)) { + lua_pop(L, 1); + lua_pushinteger(L, -1); + lua_pushstring(L, "Output file path is same as input file temporary path"); + goto copyfile_exit; + } int ret = copyfile(img.fdin, &fdout, img.size, diff --git a/include/util.h b/include/util.h index 3c2a42f..2f5c7ad 100644 --- a/include/util.h +++ b/include/util.h @@ -230,6 +230,7 @@ int read_lines_notify(int fd, char *buf, int buf_size, int *buf_offset, LOGLEVEL level); long long get_output_size(struct img_type *img, bool strict); bool img_check_free_space(struct img_type *img, int fd); +bool check_same_file(int fd1, int fd2); /* Decryption key functions */ int load_decryption_key(char *fname);