From nobody Wed Apr 17 02:04:04 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VK42r31nDz5HZ7D; Wed, 17 Apr 2024 02:04:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VK42r1M0Lz4G2p; Wed, 17 Apr 2024 02:04:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713319444; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=97Dn+T8G72equJzqJOcepPBQi6prjV12Pos35nKKvKY=; b=E6HaU9OrUhtBW2twwWqe2Qn/tucJnC0DhKeRNnhIFEzt4q1DENJcieN/1sVb3iUQpZ4Zbt 1itw1MSpl0wwu23f/pLKRGbVy2s0+7fvLNwDgLFnDERDqEr55oRNU+HJcrvsyK7gQ4Q1Xo X94qzl+p/0IhL/k/ZhI3WNZPCsEmlOLbVw+uUMOoYy3RqnMB0n+lAhkHih/aJb6MgJXqJa CW6jCLqhzYBcYtNimHuZBPn+xW6eSzZVlNI0/YHq1feHz20VfbyzP72B1j/nqau5JbWx3h 8WeeNVqL/16Z5ukMAqqdtwVlFLhaQvAF1RudK3QeEzCEWf2dprxVwzCvd6mO2Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713319444; a=rsa-sha256; cv=none; b=hewwI8iBVg1DDLabiwhzKdCI4FBRXnALiAvWH9UuMEs44sxpUp8RzvvZcUiMhPgEb1b1yw x0ZLE/GtqVDF82oWJwXMjkOxPRRLG+o8MLUeylHx43uIdAcCi31g594MXnEpFZYnNo/wfi KEZqEZduxzfv5qskib1am2fF2Dcoc/JV/wqH/YR/WaUmanEtTYqYcXt2aWu/pIPua6ztg9 DYuBi9sah8sD4tNCyPjoOuflM0tqAoRYWVq0NdmlGhIdFAffd9czWVLi2uB7yzZEfInIvG yCJV6MCGL4kxm/eV4p5ptaPxq5edDYIeB35qA9741rfh+pClbS+TeONwRPI1FQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713319444; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=97Dn+T8G72equJzqJOcepPBQi6prjV12Pos35nKKvKY=; b=kUMhHw7wE4qoP7ASUE3ReS5Y8KlmwN446jEXUeSNvBxpNTdhjZY7KIvaEZyzyVINccfPS8 XMlYhtHvNXZKQ2TrKcfyeE/x6ULbPPaDniJZuUfUvJjgXLzyqQDsKynYwqa9c/Te+6MO+7 HotnO5WPAtLvuWlLCzhXGJcJ/k6QL7FS4Zc0eS30u9t9+3/XCoJk54Q5Fv+fuwvhH75Vrs ilMIFEo6DcOTH/CxIv2GwSQUzjkbx7dTK5ccYo381XOlys3+xyvpNme1F9cdoLzUvpxlzo ujQ/ieYlZuUynhiaivgaZO2+9Uuo0805n0A2jSFUH7d8jiD7yijeP9JYOeB7jg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VK42r0y34z1746; Wed, 17 Apr 2024 02:04:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43H244Xv033929; Wed, 17 Apr 2024 02:04:04 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43H244oL033926; Wed, 17 Apr 2024 02:04:04 GMT (envelope-from git) Date: Wed, 17 Apr 2024 02:04:04 GMT Message-Id: <202404170204.43H244oL033926@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 9075d4cfad5b - main - cp: Additional sanity check. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 9075d4cfad5b339aabdf8033623a2164898c2786 Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=9075d4cfad5b339aabdf8033623a2164898c2786 commit 9075d4cfad5b339aabdf8033623a2164898c2786 Author: Dag-Erling Smørgrav AuthorDate: 2024-04-17 01:36:26 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-04-17 02:03:28 +0000 cp: Additional sanity check. Once we've successfully opened the file we've been asked to copy, check that it's of the same type as FTS told us it was. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: allanjude, markj Differential Revision: https://reviews.freebsd.org/D44806 --- bin/cp/utils.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/bin/cp/utils.c b/bin/cp/utils.c index 6c99d4c63f4a..d102fb076139 100644 --- a/bin/cp/utils.c +++ b/bin/cp/utils.c @@ -100,21 +100,34 @@ copy_fallback(int from_fd, int to_fd) int copy_file(const FTSENT *entp, int dne) { - struct stat *fs; + struct stat sb, *fs; ssize_t wcount; off_t wtotal; int ch, checkch, from_fd, rval, to_fd; int use_copy_file_range = 1; + fs = entp->fts_statp; from_fd = to_fd = -1; - if (!lflag && !sflag && - (from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) { - warn("%s", entp->fts_path); - return (1); + if (!lflag && !sflag) { + if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) < 0 || + fstat(from_fd, &sb) != 0) { + warn("%s", entp->fts_path); + return (1); + } + /* + * Check that the file hasn't been replaced with one of a + * different type. This can happen if we've been asked to + * copy something which is actively being modified and + * lost the race, or if we've been asked to copy something + * like /proc/X/fd/Y which stat(2) reports as S_IFREG but + * is actually something else once you open it. + */ + if ((sb.st_mode & S_IFMT) != (fs->st_mode & S_IFMT)) { + warnx("%s: File changed", entp->fts_path); + return (1); + } } - fs = entp->fts_statp; - /* * If the file exists and we're interactive, verify with the user. * If the file DNE, set the mode to be the from file, minus setuid