From patchwork Tue Dec 4 23:44:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10712825 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 425A115A6 for ; Tue, 4 Dec 2018 23:44:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3490B2BBEC for ; Tue, 4 Dec 2018 23:44:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 32DA62BC52; Tue, 4 Dec 2018 23:44:51 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,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 D22CB2BBEC for ; Tue, 4 Dec 2018 23:44:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725979AbeLDXou (ORCPT ); Tue, 4 Dec 2018 18:44:50 -0500 Received: from mail-pf1-f194.google.com ([209.85.210.194]:43780 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725886AbeLDXou (ORCPT ); Tue, 4 Dec 2018 18:44:50 -0500 Received: by mail-pf1-f194.google.com with SMTP id w73so9013386pfk.10 for ; Tue, 04 Dec 2018 15:44:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=9DX8afGv1w+7f65QwpbQigOVELaOuR1amOAtR8RjUg0=; b=c+ueTC6VXQdxdWIAp1a6cmzuVy5KFu6tilhwQzSAsPOdfdEmFJtHYGY/oczSlhzM4a H4e1LU3v3Kr7m7Cy82xL7jpWIDHbtE6lUouo8+z9Qwp4cFyyjZWga19LGFvl+cP9QNzb SsMwsIQjg4iGV6J5IpDyRefPJieLqQYC/8yyc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=9DX8afGv1w+7f65QwpbQigOVELaOuR1amOAtR8RjUg0=; b=ZqG79s2+eFbz7l5mlrDaBm0jhvWX9ahBPVQ/cxWx/cRxquzmnDU2dEkE1u38f2Mex5 KnCaumBCgra2ySfDp4TclkUKDHyUpyijUwtqJXfwgUyL98Ninky6DnEgU/x7/gosbd+d le8aMDGM1dlRkPqoiWfkES0e9WFWu1eXb5B71vabdQtCOOHJaJxGUEqdvPKvqiqzPH0d fT+U+pIj6+tZ4neDNIIAQjzJIy/jndrSkrr6TV3OvOwFs40A9hMXt73M3N9Lys/uszsv eUa6sgI/bGDvShBB/3mwW1iM+/MmnzWIosJvIkWC54CmEuNAtkQaMHpBdnW5lpj4AjNd WHPg== X-Gm-Message-State: AA+aEWZZ8133wwPs+x8z0+i5NkFaz91c0II8uWUWQ6AosLDAFy+hRXj5 RjnJkishwfP87EmGxkBWl/IAaGVuA/Kp4Q== X-Google-Smtp-Source: AFSGD/XtRtcBO94nyo7Y8fklBV+AEWss4QCy0cUQkGTa+mMgEq4tgkSCP53WyCtfrj8sF6r7aIv5KQ== X-Received: by 2002:a63:86c2:: with SMTP id x185mr16974330pgd.230.1543967089176; Tue, 04 Dec 2018 15:44:49 -0800 (PST) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id m20sm20351909pgv.93.2018.12.04.15.44.47 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 04 Dec 2018 15:44:47 -0800 (PST) Date: Tue, 4 Dec 2018 15:44:46 -0800 From: Kees Cook To: Jan Kara Cc: Heinrich Schuchardt , Amir Goldstein , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] fanotify: Make sure to check event_len when copying Message-ID: <20181204234446.GA4676@beast> MIME-Version: 1.0 Content-Disposition: inline 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 As a precaution, make sure we check event_len when copying to userspace. Based on old feedback: https://lkml.kernel.org/r/542D9FE5.3010009@gmx.de Signed-off-by: Kees Cook --- fs/notify/fanotify/fanotify_user.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index e03be5071362..d9484a0ac6b3 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -206,7 +206,7 @@ static int process_access_response(struct fsnotify_group *group, static ssize_t copy_event_to_user(struct fsnotify_group *group, struct fsnotify_event *event, - char __user *buf) + char __user *buf, size_t count) { struct fanotify_event_metadata fanotify_event_metadata; struct file *f; @@ -220,6 +220,12 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, fd = fanotify_event_metadata.fd; ret = -EFAULT; + /* + * Sanity check copy size in case get_one_event() and + * fill_event_metadata() event_len sizes ever get out of sync. + */ + if (WARN_ON_ONCE(fanotify_event_metadata.event_len > count)) + goto out_close_fd; if (copy_to_user(buf, &fanotify_event_metadata, fanotify_event_metadata.event_len)) goto out_close_fd; @@ -295,7 +301,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf, continue; } - ret = copy_event_to_user(group, kevent, buf); + ret = copy_event_to_user(group, kevent, buf, count); if (unlikely(ret == -EOPENSTALE)) { /* * We cannot report events with stale fd so drop it.