From patchwork Thu Feb 23 14:52:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gruenbacher X-Patchwork-Id: 9588439 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 70EFE601AE for ; Thu, 23 Feb 2017 15:17:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6017128862 for ; Thu, 23 Feb 2017 15:17:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 541572886D; Thu, 23 Feb 2017 15:17:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 030B928862 for ; Thu, 23 Feb 2017 15:17:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751173AbdBWPR3 (ORCPT ); Thu, 23 Feb 2017 10:17:29 -0500 Received: from mx1.redhat.com ([209.132.183.28]:43512 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751070AbdBWPR2 (ORCPT ); Thu, 23 Feb 2017 10:17:28 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D92637FB7C; Thu, 23 Feb 2017 14:52:17 +0000 (UTC) Received: from nux.redhat.com (ovpn-117-133.ams2.redhat.com [10.36.117.133]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NEqFa6001137; Thu, 23 Feb 2017 09:52:16 -0500 From: Andreas Gruenbacher To: Al Viro , Miklos Szeredi Cc: Andreas Gruenbacher , linux-fsdevel@vger.kernel.org, "J . Bruce Fields" Subject: [PATCH] xattr: Additional maximum size check Date: Thu, 23 Feb 2017 15:52:14 +0100 Message-Id: <1487861534-14429-1-git-send-email-agruenba@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 23 Feb 2017 14:52:18 +0000 (UTC) Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When querying for the buffer size required, a filesystem's getxattr xattr handler or listxattr iop may return a value bigger than the maximum size limit. However, the VFS will never return oversize buffers, so cast such values to -E2BIG. Signed-off-by: Andreas Gruenbacher --- fs/xattr.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/fs/xattr.c b/fs/xattr.c index 7e3317c..c19a163 100644 --- a/fs/xattr.c +++ b/fs/xattr.c @@ -537,16 +537,18 @@ getxattr(struct dentry *d, const char __user *name, void __user *value, } error = vfs_getxattr(d, kname, kvalue, size); + if (error > XATTR_SIZE_MAX || + (error == -ERANGE && size >= XATTR_SIZE_MAX)) { + /* The file system tried to returned a value bigger + than XATTR_SIZE_MAX bytes. Not possible. */ + error = -E2BIG; + } if (error > 0) { if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) || (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) posix_acl_fix_xattr_to_user(kvalue, size); if (size && copy_to_user(value, kvalue, error)) error = -EFAULT; - } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) { - /* The file system tried to returned a value bigger - than XATTR_SIZE_MAX bytes. Not possible. */ - error = -E2BIG; } kvfree(kvalue); @@ -620,14 +622,16 @@ listxattr(struct dentry *d, char __user *list, size_t size) } error = vfs_listxattr(d, klist, size); - if (error > 0) { - if (size && copy_to_user(list, klist, error)) - error = -EFAULT; - } else if (error == -ERANGE && size >= XATTR_LIST_MAX) { + if (error > XATTR_LIST_MAX || + (error == -ERANGE && size >= XATTR_LIST_MAX)) { /* The file system tried to returned a list bigger than XATTR_LIST_MAX bytes. Not possible. */ error = -E2BIG; } + if (error > 0) { + if (size && copy_to_user(list, klist, error)) + error = -EFAULT; + } kvfree(klist);