Received: by 2002:a05:6520:4d:b0:139:a872:a4c9 with SMTP id i13csp2565039lkm; Mon, 20 Sep 2021 18:51:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywZgu8vKXgTo9FqN+pwNGhVUcCMBv6xONozwWbuDL74J1Xv5aCwIAew24NUlRxhGx6Vi1p X-Received: by 2002:a6b:5f08:: with SMTP id t8mr459924iob.71.1632188965104; Mon, 20 Sep 2021 18:49:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632188965; cv=none; d=google.com; s=arc-20160816; b=cwC/Yxv7XKUPo+yPSqvcvqKlo1klrHEyN0TyVR2oUtTaPWM57lYb+4pG1znPcpdQjP lPnm6M6c6sHcbIsrMeOexxKUiX0Xpl3qRU4Ka4UXpE0j5Gqhlln9Sr3atYPHr7eY5VLh KpC373lAdYSHcX2iuIweokBwisakgsbnz1olcLIJn5HuQGYjJBxKTAnGC8zvmX8GNFI/ 4kzxIJeiStU8ZTe9KYnLq/EmpMT5ZSsusMuotZZG6GI8Cs9KfXa48JBVEj04F+S/KqEm WaXLXKAKCQv0n0cuNEFBsj552aXdTIf/ZAKHVJLfTy/1/NB6Zy1vBthvhGQPdAlrCHpJ XUdA== 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:dkim-signature; bh=IosjHm8KygFr3YZE7XrnLW+QLLL9OY4YSjCIiMQdN9I=; b=kLuuKYbdOOuycQAhiUclOrMZHLPgaF5sZ0yT6X6yrXxEirjr2nG3a0qzFQHlGggZkf 0mlPpxRtFf1hDSjUNXsXMXr4uMX7K/RyWt3f1a0sI+BbiHHPw5hE1tILjcXE+EfKYmiI TJ00h+IHAgLytgcfLmOgEOdDOr5IT0JLzETMeK1Kf1dNSG5bqDiQlH+sho07e4SccTCU EXy/U7b8SHRUVWljynpcZcJCGHls1Aq4/I2BFYQ/R/BPfQlUF7IXw/fZYLDE7bC+Zpp2 6uAQx1s9FF9HtZSsFBvYsnjpyJfvSZ6pzRbaYXgAvawHym+jtJ5Fxnsa6zVJsDzlZYso viHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jFPnfnAJ; 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=pass (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 s5si13633351ilv.155.2021.09.20.18.49.13; Mon, 20 Sep 2021 18:49:25 -0700 (PDT) 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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jFPnfnAJ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345801AbhITRpf (ORCPT + 99 others); Mon, 20 Sep 2021 13:45:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:48616 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349063AbhITRnd (ORCPT ); Mon, 20 Sep 2021 13:43:33 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D23EC61B52; Mon, 20 Sep 2021 17:09:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632157759; bh=B8GJLQ0BvE2bjh1F8hpfVOQ1W3QsL9gIAWMF1VC1k+I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jFPnfnAJDOv83SZvzbjb+tGmnnl4UARowQtSb02idD57xBpsB9gpKu49JcuOhnWwS 0KqJ2dYaEz2wPUrSDa/1tp8mXdUJc5dkGxpmm9cZfUDuxQ+1Pw0cmU7egDaFyT9KpI fcNP+5qca5SkN+UPLoh3MTDTS9XiHaLzwz9yV1vU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Eric Biggers Subject: [PATCH 4.19 106/293] fscrypt: add fscrypt_symlink_getattr() for computing st_size Date: Mon, 20 Sep 2021 18:41:08 +0200 Message-Id: <20210920163936.894472009@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163933.258815435@linuxfoundation.org> References: <20210920163933.258815435@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: Eric Biggers commit d18760560593e5af921f51a8c9b64b6109d634c2 upstream. Add a helper function fscrypt_symlink_getattr() which will be called from the various filesystems' ->getattr() methods to read and decrypt the target of encrypted symlinks in order to report the correct st_size. Detailed explanation: As required by POSIX and as documented in various man pages, st_size for a symlink is supposed to be the length of the symlink target. Unfortunately, st_size has always been wrong for encrypted symlinks because st_size is populated from i_size from disk, which intentionally contains the length of the encrypted symlink target. That's slightly greater than the length of the decrypted symlink target (which is the symlink target that userspace usually sees), and usually won't match the length of the no-key encoded symlink target either. This hadn't been fixed yet because reporting the correct st_size would require reading the symlink target from disk and decrypting or encoding it, which historically has been considered too heavyweight to do in ->getattr(). Also historically, the wrong st_size had only broken a test (LTP lstat03) and there were no known complaints from real users. (This is probably because the st_size of symlinks isn't used too often, and when it is, typically it's for a hint for what buffer size to pass to readlink() -- which a slightly-too-large size still works for.) However, a couple things have changed now. First, there have recently been complaints about the current behavior from real users: - Breakage in rpmbuild: https://github.com/rpm-software-management/rpm/issues/1682 https://github.com/google/fscrypt/issues/305 - Breakage in toybox cpio: https://www.mail-archive.com/toybox@lists.landley.net/msg07193.html - Breakage in libgit2: https://issuetracker.google.com/issues/189629152 (on Android public issue tracker, requires login) Second, we now cache decrypted symlink targets in ->i_link. Therefore, taking the performance hit of reading and decrypting the symlink target in ->getattr() wouldn't be as big a deal as it used to be, since usually it will just save having to do the same thing later. Also note that eCryptfs ended up having to read and decrypt symlink targets in ->getattr() as well, to fix this same issue; see commit 3a60a1686f0d ("eCryptfs: Decrypt symlink target for stat size"). So, let's just bite the bullet, and read and decrypt the symlink target in ->getattr() in order to report the correct st_size. Add a function fscrypt_symlink_getattr() which the filesystems will call to do this. (Alternatively, we could store the decrypted size of symlinks on-disk. But there isn't a great place to do so, and encryption is meant to hide the original size to some extent; that property would be lost.) Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20210702065350.209646-2-ebiggers@kernel.org Signed-off-by: Eric Biggers Signed-off-by: Greg Kroah-Hartman --- fs/crypto/hooks.c | 44 ++++++++++++++++++++++++++++++++++++++++ include/linux/fscrypt_notsupp.h | 6 +++++ include/linux/fscrypt_supp.h | 1 3 files changed, 51 insertions(+) --- a/fs/crypto/hooks.c +++ b/fs/crypto/hooks.c @@ -279,3 +279,47 @@ err_kfree: return ERR_PTR(err); } EXPORT_SYMBOL_GPL(fscrypt_get_symlink); + +/** + * fscrypt_symlink_getattr() - set the correct st_size for encrypted symlinks + * @path: the path for the encrypted symlink being queried + * @stat: the struct being filled with the symlink's attributes + * + * Override st_size of encrypted symlinks to be the length of the decrypted + * symlink target (or the no-key encoded symlink target, if the key is + * unavailable) rather than the length of the encrypted symlink target. This is + * necessary for st_size to match the symlink target that userspace actually + * sees. POSIX requires this, and some userspace programs depend on it. + * + * This requires reading the symlink target from disk if needed, setting up the + * inode's encryption key if possible, and then decrypting or encoding the + * symlink target. This makes lstat() more heavyweight than is normally the + * case. However, decrypted symlink targets will be cached in ->i_link, so + * usually the symlink won't have to be read and decrypted again later if/when + * it is actually followed, readlink() is called, or lstat() is called again. + * + * Return: 0 on success, -errno on failure + */ +int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat) +{ + struct dentry *dentry = path->dentry; + struct inode *inode = d_inode(dentry); + const char *link; + DEFINE_DELAYED_CALL(done); + + /* + * To get the symlink target that userspace will see (whether it's the + * decrypted target or the no-key encoded target), we can just get it in + * the same way the VFS does during path resolution and readlink(). + */ + link = READ_ONCE(inode->i_link); + if (!link) { + link = inode->i_op->get_link(dentry, inode, &done); + if (IS_ERR(link)) + return PTR_ERR(link); + } + stat->size = strlen(link); + do_delayed_call(&done); + return 0; +} +EXPORT_SYMBOL_GPL(fscrypt_symlink_getattr); --- a/include/linux/fscrypt_notsupp.h +++ b/include/linux/fscrypt_notsupp.h @@ -234,4 +234,10 @@ static inline const char *fscrypt_get_sy return ERR_PTR(-EOPNOTSUPP); } +static inline int fscrypt_symlink_getattr(const struct path *path, + struct kstat *stat) +{ + return -EOPNOTSUPP; +} + #endif /* _LINUX_FSCRYPT_NOTSUPP_H */ --- a/include/linux/fscrypt_supp.h +++ b/include/linux/fscrypt_supp.h @@ -231,5 +231,6 @@ extern int __fscrypt_encrypt_symlink(str extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr, unsigned int max_size, struct delayed_call *done); +int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat); #endif /* _LINUX_FSCRYPT_SUPP_H */