From patchwork Mon Apr 18 19:09:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mimi Zohar X-Patchwork-Id: 8874541 Return-Path: X-Original-To: patchwork-linux-fsdevel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id A9C449F36E for ; Mon, 18 Apr 2016 19:10:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D7C94201C8 for ; Mon, 18 Apr 2016 19:10:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E6BC520115 for ; Mon, 18 Apr 2016 19:10:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751736AbcDRTKQ (ORCPT ); Mon, 18 Apr 2016 15:10:16 -0400 Received: from e28smtp05.in.ibm.com ([125.16.236.5]:57641 "EHLO e28smtp05.in.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751441AbcDRTKE (ORCPT ); Mon, 18 Apr 2016 15:10:04 -0400 Received: from localhost by e28smtp05.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 19 Apr 2016 00:39:57 +0530 Received: from d28relay04.in.ibm.com (9.184.220.61) by e28smtp05.in.ibm.com (192.168.1.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 19 Apr 2016 00:39:54 +0530 X-IBM-Helo: d28relay04.in.ibm.com X-IBM-MailFrom: zohar@linux.vnet.ibm.com X-IBM-RcptTo: linux-fsdevel@vger.kernel.org; linux-kernel@vger.kernel.org; linux-security-module@vger.kernel.org Received: from d28av03.in.ibm.com (d28av03.in.ibm.com [9.184.220.65]) by d28relay04.in.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u3IJ9pMl16646604; Tue, 19 Apr 2016 00:39:52 +0530 Received: from d28av03.in.ibm.com (localhost [127.0.0.1]) by d28av03.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u3IJ9qf3024279; Tue, 19 Apr 2016 00:39:53 +0530 Received: from dhcp-9-2-50-178.watson.ibm.com (dhcp-9-2-50-178.watson.ibm.com [9.2.50.178]) by d28av03.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u3IJ9oij024216; Tue, 19 Apr 2016 00:39:51 +0530 Message-ID: <1461006589.2423.3.camel@linux.vnet.ibm.com> Subject: [PATCH v2] fs: define a string representation of the kernel_read_file_id enumeration From: Mimi Zohar To: Kees Cook Cc: Al Viro , linux-security-module , linux-kernel , linux-fsdevel Date: Mon, 18 Apr 2016 15:09:49 -0400 X-Mailer: Evolution 3.12.11 (3.12.11-1.fc21) Mime-Version: 1.0 X-TM-AS-MML: disable x-cbid: 16041819-0017-0000-0000-00001E173203 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP A string representation of the kernel_read_file_id enumeration is needed for displaying messages (eg. pr_info, auditing) that can be used by multiple LSMs and the integrity subsystem. To simplify keeping the list of strings up to date with the enumeration, this patch defines two new preprocessing macros named __fid_enumify and __fid_stringify to create the enumeration and an array of strings. kernel_read_file_id_str() returns a string based on the enumeration. Changelog: - redefined the macros and simplified their usage - James Bottomley Signed-off-by: Mimi Zohar --- fs/exec.c | 19 ------------------- include/linux/fs.h | 32 +++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 05e71b6..c4010b8 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -819,25 +819,6 @@ struct file *open_exec(const char *name) } EXPORT_SYMBOL(open_exec); -const char *kernel_read_file_id_str(enum kernel_read_file_id id) -{ - switch (id) { - case READING_FIRMWARE: - return "firmware"; - case READING_MODULE: - return "kernel-module"; - case READING_KEXEC_IMAGE: - return "kexec-image"; - case READING_KEXEC_INITRAMFS: - return "kexec-initramfs"; - case READING_POLICY: - return "security-policy"; - default: - return "unknown"; - } -} -EXPORT_SYMBOL(kernel_read_file_id_str); - int kernel_read(struct file *file, loff_t offset, char *addr, unsigned long count) { diff --git a/include/linux/fs.h b/include/linux/fs.h index 23ea886..497c17f 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2580,16 +2580,34 @@ static inline void i_readcount_inc(struct inode *inode) #endif extern int do_pipe_flags(int *, int); +#define __kernel_read_file_id(id) \ + id(UNKNOWN, unknown) \ + id(FIRMWARE, firmware) \ + id(MODULE, kernel-module) \ + id(KEXEC_IMAGE, kexec-image) \ + id(KEXEC_INITRAMFS, kexec-initramfs) \ + id(POLICY, security-policy) \ + id(MAX_ID, ) + +#define __fid_enumify(ENUM, dummy) READING_ ## ENUM, +#define __fid_stringify(dummy, str) #str, + enum kernel_read_file_id { - READING_FIRMWARE = 1, - READING_MODULE, - READING_KEXEC_IMAGE, - READING_KEXEC_INITRAMFS, - READING_POLICY, - READING_MAX_ID + __kernel_read_file_id(__fid_enumify) +}; + +static const char *kernel_read_file_str[] = { + __kernel_read_file_id(__fid_stringify) }; -extern const char *kernel_read_file_id_str(enum kernel_read_file_id id); +static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id) +{ + if (id < 0 || id >= READING_MAX_ID) + return kernel_read_file_str[READING_UNKNOWN]; + + return kernel_read_file_str[id]; +} + extern int kernel_read(struct file *, loff_t, char *, unsigned long); extern int kernel_read_file(struct file *, void **, loff_t *, loff_t, enum kernel_read_file_id);