From patchwork Fri Oct 7 20:35:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Owens X-Patchwork-Id: 9367481 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 08CDA6075E for ; Fri, 7 Oct 2016 20:36:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EF43329319 for ; Fri, 7 Oct 2016 20:36:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E30B0297F9; Fri, 7 Oct 2016 20:36:01 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 6EC3829319 for ; Fri, 7 Oct 2016 20:36:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932284AbcJGUf6 (ORCPT ); Fri, 7 Oct 2016 16:35:58 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:34122 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751640AbcJGUf5 (ORCPT ); Fri, 7 Oct 2016 16:35:57 -0400 Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.16.0.17/8.16.0.17) with SMTP id u97KW4MO023498 for ; Fri, 7 Oct 2016 13:35:56 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=facebook; bh=O/KLsaZGUSPj2Yr7GAvodni83C7qmKXKcNkBXJMLGiM=; b=e45b2rbgy9ep1s68C4d/bftJWf9jjSHx05Z1319EYFiF9YVrWStiwc6V3RXRoVIQpl/v dWERovXQaCp+V8v7WzC9aIjH5a1o1jx9YptzN1WI1uYtsmp61qVNj1XpRhOnIYNvUcGD TldV3jjQ7w7aTbHliVMlvkeHk4H79LN1LYo= Received: from mail.thefacebook.com ([199.201.64.23]) by m0001303.ppops.net with ESMTP id 25xfevh6sj-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Fri, 07 Oct 2016 13:35:56 -0700 Received: from mx-out.facebook.com (192.168.52.123) by PRN-CHUB06.TheFacebook.com (192.168.16.16) with Microsoft SMTP Server (TLS) id 14.3.294.0; Fri, 7 Oct 2016 13:35:55 -0700 Received: from facebook.com (2401:db00:11:d0be:face:0:1b:0) by mx-out.facebook.com (10.212.232.59) with ESMTP id a541fc0c8ccd11e692c80002c991e86a-321eea50 for ; Fri, 07 Oct 2016 13:35:55 -0700 Received: by devbig337.prn1.facebook.com (Postfix, from userid 10532) id 51DB765E142E; Fri, 7 Oct 2016 13:35:54 -0700 (PDT) From: Calvin Owens To: Jeff Layton , "J. Bruce Fields" , Rusty Russell CC: , , , Calvin Owens Subject: [PATCH] fs: Assert on module file_operations without an owner Date: Fri, 7 Oct 2016 13:35:52 -0700 Message-ID: <48414ef29337b54e2a842bd841f73f01ab74ebe7.1475872278.git.calvinowens@fb.com> X-Mailer: git-send-email 2.9.3 X-FB-Internal: Safe MIME-Version: 1.0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-10-07_09:, , signatures=0 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 Omitting the owner field in file_operations declared in modules is an easy mistake to make, and can result in crashes when the module is unloaded while userspace is poking the file. This patch modifies fops_get() to WARN when it encounters a NULL owner, since in this case it cannot take a reference on the containing module. Signed-off-by: Calvin Owens --- include/linux/fs.h | 13 ++++++++++++- kernel/module.c | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index 901e25d..fafda9e 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2081,10 +2081,21 @@ extern struct dentry *mount_pseudo(struct file_system_type *, char *, unsigned long); /* Alas, no aliases. Too much hassle with bringing module.h everywhere */ -#define fops_get(fops) \ +#define __fops_get(fops) \ (((fops) && try_module_get((fops)->owner) ? (fops) : NULL)) #define fops_put(fops) \ do { if (fops) module_put((fops)->owner); } while(0) + +#define unowned_fmt "No fops owner at %p in [%s]\n" +#define fops_unowned(fops) \ + (is_module_address((unsigned long)(fops)) && !(fops)->owner) +#define fops_modname(fops) \ + __module_address((unsigned long)(fops))->name +#define fops_warn_unowned(fops) \ + WARN(fops_unowned(fops), unowned_fmt, (fops), fops_modname(fops)) +#define fops_get(fops) \ + ({ fops_warn_unowned(fops); __fops_get(fops); }) + /* * This one is to be used *ONLY* from ->open() instances. * fops must be non-NULL, pinned down *and* module dependencies diff --git a/kernel/module.c b/kernel/module.c index 529efae..4443727 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -4181,6 +4181,7 @@ bool is_module_address(unsigned long addr) return ret; } +EXPORT_SYMBOL_GPL(is_module_address); /* * __module_address - get the module which contains an address.