From: Chuck Lever <chuck.lever@xxxxxxxxxx> commit 42c3732fa8073717dd7d924472f1c0bc5b452fdc upstream. De-duplicate the same functionality in several places by hoisting the is_dot_dotdot() utility function into linux/fs.h. Suggested-by: Amir Goldstein <amir73il@xxxxxxxxx> Reviewed-by: Jeff Layton <jlayton@xxxxxxxxxx> Reviewed-by: Amir Goldstein <amir73il@xxxxxxxxx> Acked-by: Christian Brauner <brauner@xxxxxxxxxx> Signed-off-by: Chuck Lever <chuck.lever@xxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- fs/crypto/fname.c | 8 +------- fs/ecryptfs/crypto.c | 10 ---------- fs/f2fs/f2fs.h | 11 ----------- fs/namei.c | 6 ++---- include/linux/fs.h | 11 +++++++++++ 5 files changed, 14 insertions(+), 32 deletions(-) --- a/fs/crypto/fname.c +++ b/fs/crypto/fname.c @@ -74,13 +74,7 @@ struct fscrypt_nokey_name { static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) { - if (str->len == 1 && str->name[0] == '.') - return true; - - if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.') - return true; - - return false; + return is_dot_dotdot(str->name, str->len); } /** --- a/fs/ecryptfs/crypto.c +++ b/fs/ecryptfs/crypto.c @@ -1973,16 +1973,6 @@ out: return rc; } -static bool is_dot_dotdot(const char *name, size_t name_size) -{ - if (name_size == 1 && name[0] == '.') - return true; - else if (name_size == 2 && name[0] == '.' && name[1] == '.') - return true; - - return false; -} - /** * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext * @plaintext_name: The plaintext name --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -3335,17 +3335,6 @@ static inline bool f2fs_cp_error(struct return is_set_ckpt_flags(sbi, CP_ERROR_FLAG); } -static inline bool is_dot_dotdot(const u8 *name, size_t len) -{ - if (len == 1 && name[0] == '.') - return true; - - if (len == 2 && name[0] == '.' && name[1] == '.') - return true; - - return false; -} - static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi, size_t size, gfp_t flags) { --- a/fs/namei.c +++ b/fs/namei.c @@ -2663,10 +2663,8 @@ static int lookup_one_common(struct user if (!len) return -EACCES; - if (unlikely(name[0] == '.')) { - if (len < 2 || (len == 2 && name[1] == '.')) - return -EACCES; - } + if (is_dot_dotdot(name, len)) + return -EACCES; while (len--) { unsigned int c = *(const unsigned char *)name++; --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3107,6 +3107,17 @@ extern bool path_is_under(const struct p extern char *file_path(struct file *, char *, int); +/** + * is_dot_dotdot - returns true only if @name is "." or ".." + * @name: file name to check + * @len: length of file name, in bytes + */ +static inline bool is_dot_dotdot(const char *name, size_t len) +{ + return len && unlikely(name[0] == '.') && + (len == 1 || (len == 2 && name[1] == '.')); +} + #include <linux/err.h> /* needed for stackable file system support */ Patches currently in stable-queue which might be from chuck.lever@xxxxxxxxxx are queue-6.1/nfsd-fix-refcount-leak-when-file-is-unhashed-after-b.patch queue-6.1/nfsd-return-einval-when-namelen-is-0.patch queue-6.1/selinux-smack-don-t-bypass-permissions-check-in-inode_setsecctx-hook.patch queue-6.1/nfsd-remove-unneeded-eexist-error-check-in-nfsd_do_f.patch queue-6.1/fs-create-a-generic-is_dot_dotdot-utility.patch queue-6.1/nfsd-call-cache_put-if-xdr_reserve_space-returns-nul.patch