From patchwork Sat Jun 29 22:47:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Hilliard X-Patchwork-Id: 1954291 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=DcVPi8ki; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=S/6WJDnI; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::43e; helo=mail-pf1-x43e.google.com; envelope-from=swupdate+bncbcl4hcw73qcbb5m5qk2amgqekacllkq@googlegroups.com; receiver=patchwork.ozlabs.org) Received: from mail-pf1-x43e.google.com (mail-pf1-x43e.google.com [IPv6:2607:f8b0:4864:20::43e]) (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 4WBS9r5k6cz20Xg for ; Sun, 30 Jun 2024 08:47:27 +1000 (AEST) Received: by mail-pf1-x43e.google.com with SMTP id d2e1a72fcca58-70663dee272sf1446287b3a.0 for ; Sat, 29 Jun 2024 15:47:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1719701239; cv=pass; d=google.com; s=arc-20160816; b=b1i/982mijNF53VnJ6kGUMvNYEFZNRipPqMp3DYBAYAdqr4I8gjafrNJZS+vBp5AqV xYwsuEZbcc+C4+9AoiGdUViJ8w0axdQkMG4IMaFNvimUDPNvui7TzLyGHjdZ1luiQHw1 SEFj1wtVQA2msYhXsNI64sBaI+4MxU/gFRQRE//VXRrTTNmBaDclIf/SLCuqyxkQvHF7 E9PsHZHnlgVvczo3OpWrgbVYO2SnKjwfmhe8oDO/sAY1yQzt8Bp31BOmHWtfcRiW1BtL 7V0bIZG6JrVbXXig4VXflw7QWImTc1osAlxoGkOSAcj9dSdjE0BdbOs0OSp2+C+OmAr6 K8bw== 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=iW7ESEInN1Had7dxWmpYqTVxCqTG1PHumtGy9j3X138=; fh=lhqbSduFk/6quSP+d6Nb+5hPL5fs8ndx8iuHNPlUhnE=; b=Rq8zr2sPHXK6HxCnsPvgPPxh1YWQC+bmKOx60cXKZvnh67+lFnpt22r46E5L6rO8eB wCrgL78NhVaXtLIEXc9dTmpRweHUTb5QJvlcuEWZqRmqfPDLWyaf9EyX3LtTRLfLX7hQ O7amzmCH7+Zn1lCIVLGCnqg1n469b2/qfqbhxP/A6zFTs9PkBFVxK/llEle9xlxyqF/L GorkVi9A/D0e3Mg/4hxLzerZMrrGMGnhVo3Q5APKUBstiNBN+cjbcHC8HGZ6jhuQ53wW sm/jQCyUwofyBupkZ6mQ6oh0nnwqAHVRvSFkxd2fqLiDQFFw+iFbXyoJCJjxLBk3OkNH 6KWw==; darn=patchwork.ozlabs.org ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JLZLaR0k; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d2a 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=1719701239; x=1720306039; 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=iW7ESEInN1Had7dxWmpYqTVxCqTG1PHumtGy9j3X138=; b=DcVPi8ki+AOyDmQE4M4F42ayK78UwG230rt99D/89zmZTfz81ZwZcTkaqD/A9LxZdQ 6wKEoC7/RSt8elhmqguuLVRKGHDbUn9KhtTSkUz+r1eU/xBlG4o6i4xmi2FcGgOeLEaw tEm6QOp+5Z2sgh+LrzJ+Oo//uISOQ+iLclOJmMebKPIUhcOF7m0DnxktlyT6cGORT4ou Z3Ohlwv3hy+MRzQW3Mb0+PHrj/rvmKkH6mzt5ZFUiFkzM7Ff9yn0D62MugrNUU1Qw49K AG8hg52IVgU+gl65/f3OrHjhT5o05hZQ6/6QdY3a+e1uGnALTGsEfTp8ScRRpR/fQX38 Scpg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719701239; x=1720306039; 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=iW7ESEInN1Had7dxWmpYqTVxCqTG1PHumtGy9j3X138=; b=S/6WJDnIeWtau98C48bkmivLHbhs2RVEZ4lZoUTZnQDX9F+2CO/mOrAbo0cahlWliA y4NYPNk7aIkSTQupsQH4yMTyznIHnUgnldlsLH0Oc4cqz4Vkso+hs5fSU0a+g9jskHlv O1zI3HYk5HN12B4ImAU/US+LGMV0uoNdWDDRtKqTT7UW2s8rcTe9RCBRwe8DfKg/OU3y UFXtkVikveFOfvBTQFkyquM6QHV+WzhgJC71EWLfBqiEsaN8OegvtOpvDI8htJUm7wNq j8L8kH9cx4hTXuwHjc7tYqdyucISdfqSPKrjGA3jBqMVr9epdVRGNT2LNWXG93kzQYGR PYag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719701239; x=1720306039; 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=iW7ESEInN1Had7dxWmpYqTVxCqTG1PHumtGy9j3X138=; b=ly4f12+08QenX2cSPKr/3Ksdr97TiWOTUTDQgztLrJ3hWKMbXkryYA8X3pdrDIS/Gk pX7AdnKAilZ/9fr0gG9SFszrsCP0PDQ9uVbGO6XVlcVjao+HcQAb3irV65LYg3qVod1N C1Q9mklm6/aJNk6jJwGZEMsotH2nBZ+CJzWmN5dAyDveg8qr/KpiJZDK2gcp67r1vwJo HXbq+51bCwMUk+di6nqAF4AeYvT+JLdp9I+N23vxRP1F+3CxyjA8JMNbUoj/ncdiuBih dYwNIJQwPa7ZeRdpkNXaVdSu6D5s/ht3I6zXcd8NBvjVwfDEztNjeElUre9JzTDijTNd ZDuQ== Sender: swupdate@googlegroups.com X-Forwarded-Encrypted: i=2; AJvYcCUtKmZbL9WAh9DreY/uWm8/E1UlTIlULyi+qRKqCCLB+M+R8kABRKmPflw3vpgiiVK443yilqz5zKYgR5cfO/BwXX74GGJVSAKtVENfBA== X-Gm-Message-State: AOJu0YyqAldKkPTOMC8cFpsnl7eP5aYqW8UyhjJRpnylkHeftKaJoaEN /aI/l4m1fu9suYp8l+qCWu8/OWFRajlidwGrrgNpQObAU4h63VNf X-Google-Smtp-Source: AGHT+IEtpp+wJEprW3nMIG1Zdx2sJyigiW9xQEDwaEbvjj2U9ItIsvnNioUuKb8CyX/xVpFmJe3HtA== X-Received: by 2002:a05:6a00:b82:b0:708:11f:d151 with SMTP id d2e1a72fcca58-70aaad6214fmr2866361b3a.16.1719701238422; Sat, 29 Jun 2024 15:47:18 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a05:6a00:3e0e:b0:702:6dc7:234f with SMTP id d2e1a72fcca58-7081aa0e39als1249054b3a.2.-pod-prod-06-us; Sat, 29 Jun 2024 15:47:17 -0700 (PDT) X-Received: by 2002:a05:6a20:9144:b0:1bd:2c36:afd6 with SMTP id adf61e73a8af0-1bef60fce4fmr3585814637.2.1719701236813; Sat, 29 Jun 2024 15:47:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1719701236; cv=none; d=google.com; s=arc-20160816; b=h6V1kR2niO9DSOqKIRiPE4fnd2zqAGbjUd3l6LS+1dDaFkhYoIQlDRAvrQmiZeQfpj 9ctkIkKXNEtMlQ6dsEcAFiQwKfsQt7GBr/6LgM49nxdGARBKSMV1xe7nVDa5uXiWBT3C +EEdVVXZoPW1P7+R+BtwH/w74vQnGIpb9hveEPA08H1qPkk9HuWGIJM2CQ7qhtlP3cme 3IXW3pg8wF+Of2GkgZsdnamoFCCaPga/7Sb7Q3FD2TXouSgobbZSJ/tViYjNchUe43z9 NQKvZ5xMZDq2XJ4rGEpdUKRo7OfOMKXH8Gu1UEyluJSm+gew15Jq1q6zofK4+q19Jt3v gYpg== 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=uJbfNdIKumEtQT9RnlT7yn6ZtZxKhZs1mADP0KrmDa0=; fh=KPrAw848PoyEggiIX82ubXk7sJG2UD396EeWf1FQVUo=; b=s9uolLPjQF1PMVEZdgKGmzWTV624nq+0dDEg1yR/SV1yTnUrmYe4nlrVkV+r0xjt2b 8k5m+1LoYj2/Esak/tVQs6b7iLm98HM8XVik/1PfvBuoxlH4xfPeRbj38Gr8o+AT6kX4 Lj807A+vr5Bbc8TNBppUwUVSmvd9F4kjUYjm3lAG9D2xrFHu4nGJWj3PDTRUPLfawpNK Pzf18llpNl6jTEax3eOJL8lHgjSaIZVC72VM95hAW/yOzWST8YDwpgpFIv3op4tYmH9X eac7RGpahULpkSivXGf6Wa6iT9r5pVGZTUDDHz7WBAXuBtgrlvSKqZncDgb81gm6DOnz cGDg==; dara=google.com ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JLZLaR0k; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d2a as permitted sender) smtp.mailfrom=james.hilliard1@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com. [2607:f8b0:4864:20::d2a]) by gmr-mx.google.com with ESMTPS id 41be03b00d2f7-72c72c6890csi198695a12.5.2024.06.29.15.47.16 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 29 Jun 2024 15:47:16 -0700 (PDT) Received-SPF: pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d2a as permitted sender) client-ip=2607:f8b0:4864:20::d2a; Received: by mail-io1-xd2a.google.com with SMTP id ca18e2360f4ac-7e21dfbc310so67160039f.1 for ; Sat, 29 Jun 2024 15:47:16 -0700 (PDT) X-Received: by 2002:a6b:e517:0:b0:7f6:18ce:2611 with SMTP id ca18e2360f4ac-7f62ee1b585mr183903739f.12.1719701235831; Sat, 29 Jun 2024 15:47:15 -0700 (PDT) Received: from james-x399.localdomain (97-118-179-167.hlrn.qwest.net. [97.118.179.167]) by smtp.gmail.com with ESMTPSA id ca18e2360f4ac-7f61d2097desm118602139f.54.2024.06.29.15.47.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jun 2024 15:47:15 -0700 (PDT) From: James Hilliard To: swupdate@googlegroups.com Cc: James Hilliard Subject: [swupdate] [PATCH] util: Ensure swupdate_remove_directory unmounts path Date: Sat, 29 Jun 2024 16:47:06 -0600 Message-Id: <20240629224706.1164950-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=20230601 header.b=JLZLaR0k; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d2a 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: , In the event that the DATADST_DIR is left mounted we should ensure that it is unmounted prior to deletion, otherwise we may end up being unable to remove DATADST_DIR and/or accidentially deleting files inside the mounted filesystem. Signed-off-by: James Hilliard Reviewed-by: Stefano Babic --- core/util.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/core/util.c b/core/util.c index 70f0d28f..b8d3f3ec 100644 --- a/core/util.c +++ b/core/util.c @@ -169,6 +169,26 @@ void swupdate_create_directory(const char* path) { } #ifndef CONFIG_NOCLEANUP +static int _is_mount_point(const char *path, const char *parent_path) { + struct stat path_stat, parent_stat; + + if (!stat(path, &path_stat)) { + ERROR("stat for path %s failed: %s", path, strerror(errno)); + return -errno; + } + + if (!stat(parent_path, &parent_stat)) { + ERROR("stat for parent path %s failed: %s", parent_path, strerror(errno)); + return -errno; + } + + if (path_stat.st_dev != parent_stat.st_dev) { + return 1; + } + + return 0; +} + static int _remove_directory_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { @@ -187,7 +207,23 @@ int swupdate_remove_directory(const char* path) ERROR("OOM: Directory %s not removed", path); return -ENOMEM; } + + ret = _is_mount_point(dpath, get_tmpdir()); + if (ret < 0) + goto out; + + if (ret) { + WARN("Unexpected mountpoint, unmounting: %s", dpath); + ret = swupdate_umount(dpath); + if (ret && errno != EINVAL) { + ret = -errno; + ERROR("Can't unmount path %s: %s", dpath, strerror(errno)); + goto out; + } + } + ret = nftw(dpath, _remove_directory_cb, 64, FTW_DEPTH | FTW_PHYS); +out: free(dpath); return ret; }