From nobody Sat Apr 27 14:58:48 2024 X-Original-To: dev-commits-src-all@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 4VRXm924Xmz5JGsp; Sat, 27 Apr 2024 14:58:49 +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 4VRXm90gdbz42rk; Sat, 27 Apr 2024 14:58:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714229929; 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=j+PV8hAxpO41QDGF3mgogNU0h8qSxcOUg7eRciShvbc=; b=aaPT+1cV6x/HKb8uvrHkH2s3bNBu3pLQ4e5+AQgmoCQ3wrTwOwtfXovMTOvpSsl5/i3YVR 4ZypdHU7Xol1BA6zqgfm+GdVZ331t0fT4wijSbrFftxZ/aKBooPR9vaRJE1wpVB4h/I2vw CZJJhHm5UR9kap+jm4Or0x25Z1cjdUdPLTEXCmzQp2gf5DytRp1qs0xPGD4mJjm4ogyGjN A/zrij5GBr/vGXhSFpX5Ww48S19lQ+rc4cUZ65DLS3+z5GWBO2ZFjHCai5yKPCaX95XIMf RO0vaTfUjoeGe6Twq3Txfignz1bK3ZkM98qEaDqWUgH7nej80CHV7MV3XKnJMQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1714229929; a=rsa-sha256; cv=none; b=kzf+2GbNKID22U913gxIgs+9UFnEoTZd51ZhQUj1cbNrAjxsGvkxuZSRf7lsZcNL4+0ijZ y0hw4m3dPetJzyeiIAI+B2RWiNc38dh15wYc8DqlWTU2BUsCNKH16S6GBMxV+oUNqZ1rQF 6AyhF82FaeraNvTQHYonQSv3w9G5eqkSUOP/rFTgB5XMbvTQbfyTqf5OIcLPhzC87EkpZV ZAbf9EOLvM2bsACtEcm/iOlRPB2wDWCF7EDsLnDXiVVGMLbAChGcHsGeu2KUcxp0A22utA PUlvmpOmp4dnJtCUsmc8eUQk1Od6n4o3zKV1RNVoc40oCFpG0BOL/CBnuGLSCg== 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=1714229929; 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=j+PV8hAxpO41QDGF3mgogNU0h8qSxcOUg7eRciShvbc=; b=oInqOgCL01Bmxce74Vs/nqefakxVhhzuIAH/ZHTdWAKam6iZgafh4y8OHxewxWOAX+J2XR etdQrE+uQ8AI3woGfU864s286FQxpZo6kiBQ9/4HuG6OWbDJUnBhsE22gnci4Rr0SXSLMr 0/MpeDWt1IWFdeud8vnzCRzP4mY/Jla1fhsSneaCImBk2WrQWrcBKnVmAospoi31P2La/R /PS6rYcRHPrjvIXhdgGGqNQVoSMBnXiBtImjSq8rH5Ac4Z4w+0vRBMaw3YadkzYEodslAw mZczqLXw3gTP0arxzmPgM4eTbTGwMFShcvkAZYr9bnPaTDDm6kUCg4EjWgraFQ== 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 4VRXm868lSzWlD; Sat, 27 Apr 2024 14:58:48 +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 43REwmFI032623; Sat, 27 Apr 2024 14:58:48 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43REwmsr032620; Sat, 27 Apr 2024 14:58:48 GMT (envelope-from git) Date: Sat, 27 Apr 2024 14:58:48 GMT Message-Id: <202404271458.43REwmsr032620@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 17997507040c - stable/13 - cp: Clarify an obscure comment. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@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/stable/13 X-Git-Reftype: branch X-Git-Commit: 17997507040c4ad1c6a25ce93c3981ee19ce28c0 Auto-Submitted: auto-generated The branch stable/13 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=17997507040c4ad1c6a25ce93c3981ee19ce28c0 commit 17997507040c4ad1c6a25ce93c3981ee19ce28c0 Author: Dag-Erling Smørgrav AuthorDate: 2024-04-17 01:36:22 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-04-27 14:58:40 +0000 cp: Clarify an obscure comment. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D44805 (cherry picked from commit 64d6925d1901637125f9f739282e72c992657dc8) 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 (cherry picked from commit 9075d4cfad5b339aabdf8033623a2164898c2786) cp: Use warnc(). MFC after: 1 week Sponsored by: Klara, Inc. (cherry picked from commit f070188c3ad6b87ee9ce220b21718333d1bd9d52) --- bin/cp/cp.c | 13 +++++++++---- bin/cp/utils.c | 27 ++++++++++++++++++++------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/bin/cp/cp.c b/bin/cp/cp.c index af7d5ffac398..a8c57308ae96 100644 --- a/bin/cp/cp.c +++ b/bin/cp/cp.c @@ -316,8 +316,7 @@ copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) case FTS_NS: case FTS_DNR: case FTS_ERR: - warnx("%s: %s", - curr->fts_path, strerror(curr->fts_errno)); + warnc(curr->fts_errno, "%s", curr->fts_path); badcp = rval = 1; continue; case FTS_DC: /* Warn, continue. */ @@ -491,13 +490,19 @@ copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) switch (curr->fts_statp->st_mode & S_IFMT) { case S_IFLNK: - /* Catch special case of a non-dangling symlink. */ if ((fts_options & FTS_LOGICAL) || ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) { + /* + * We asked FTS to follow links but got + * here anyway, which means the target is + * nonexistent or inaccessible. Let + * copy_file() deal with the error. + */ if (copy_file(curr, dne)) badcp = rval = 1; - } else { + } else { + /* Copy the link. */ if (copy_link(curr, !dne)) badcp = rval = 1; } diff --git a/bin/cp/utils.c b/bin/cp/utils.c index 0bb5157e3f57..9f6ef888c7ea 100644 --- a/bin/cp/utils.c +++ b/bin/cp/utils.c @@ -106,21 +106,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