From patchwork Sun Aug 13 14:46:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SF Markus Elfring X-Patchwork-Id: 9897887 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 6C65860244 for ; Sun, 13 Aug 2017 14:47:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 48E7C2860F for ; Sun, 13 Aug 2017 14:47:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3AAEC28614; Sun, 13 Aug 2017 14:47:16 +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 C2DB92860F for ; Sun, 13 Aug 2017 14:47:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751490AbdHMOrO (ORCPT ); Sun, 13 Aug 2017 10:47:14 -0400 Received: from mout.web.de ([212.227.15.14]:59761 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751344AbdHMOrN (ORCPT ); Sun, 13 Aug 2017 10:47:13 -0400 Received: from [192.168.1.2] ([92.228.32.17]) by smtp.web.de (mrweb003 [213.165.67.108]) with ESMTPSA (Nemesis) id 0MZlNu-1e2Hq20pMk-00LTCd; Sun, 13 Aug 2017 16:46:56 +0200 Subject: [PATCH 2/4] selinux: Adjust jump targets in ebitmap_read() From: SF Markus Elfring To: linux-security-module@vger.kernel.org, selinux@tycho.nsa.gov, Alexey Dobriyan , Daniel Jurgens , Eric Paris , James Morris , Junil Lee , Paul Moore , "Serge E. Hallyn" , Stephen Smalley , William Roberts Cc: LKML , kernel-janitors@vger.kernel.org References: Message-ID: <2c4582c9-efc1-4b8c-1095-84e00062c7de@users.sourceforge.net> Date: Sun, 13 Aug 2017 16:46:53 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: Content-Language: en-GB X-Provags-ID: V03:K0:O2544z3KnxrYEwqOKpajpSthAf+ij3faTbCy7o+4wonLDUcPgcl hCjW827/+WPFByW7RQV9mnH8SkmbRr6lnV7JKRUn8MqeNdTJxW81e6rzbpCid1+B6v/BsVR 2JSXPKkxw5ebxqadlRTXN8VWFND4gffOTDaCpUGxGHHu7qpVPBcGWHipjrEke+5xultX4op yGZ1pN44eAbHIKx2LRWoQ== X-UI-Out-Filterresults: notjunk:1; V01:K0:wC3AZ/yp0F8=:vXEyHeC4Xuymld6PSveKSi KtWKUTV+XLw1I0I27n1HPjO+JERoknP6Etr/QBy4K7EdS7eNTUnKeI9J4lMBh8AA0cvqRMVr4 jL3TXe962qk8ck6tdJOq+TgKdpF9GhlYAkyqC0NZhzo/Yo8INyfRdOOMK9cYq+ZDqGJBq5YHL N+fm42tUZelHwF0hUfabq16MA6Xb2JYh3hcaasadJ73jtKOb4A8yX7ou2qBWEHKtZtP5Dgtiw TOfMWR2BixG9OSjzZMhKBwnF3ykuuIwDXObHTqSWn+Bxk7FJlFUwQbpgYSpB5l5HB7cplx18l Go39yL2WkrWDzyOp3Nmvds7WrqYCP0+iFoarWeQrVpfdGij8SMu2TEpR3GwzNqE87tNfLdVUU byFQ3HTbdj3Vyv6y+GnjBcmVsKjgRjMyhOXf1Rc8kIzTdab/nswsQkBdiwWr7sdGVV/Issx8z h9St5P4Mk6pel3AuB2zkB2bGOFVxaf7khAyoUC0Me47NKAoHCPK3QCZ79lUeNtW4VHT/HjN+A 6fJl3rg0L1q33wa/8aNR0x2poHZedKEB90SacZKZqtBZzpiXyoOdNfJwGpz4Q9rDQTXcB1OqU WTvUDOLKcXBNWxJDxIRyETQlWFVBSmEbHDjN7zt5cTV7SiMzFbxhSTKsLyOh0rb8CF89byZwU CtbkjvtDV/l53jXZ/lemcNS86FEZe+EVNoaafgthgycK7urotxg1i84gbbXkLMxzMBP0grPmZ AYroHZ3i6mqQ0ykKVXJEBt6G+aYjEpHKxdo3COGzScksTOXVWO12W+1PDboAXuVxVjTCECjyw yrz3pRZXVzXQCqVIdH2MPxvq+I9H/O8w/yloBr6gC8uz5PP3s0= Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Markus Elfring Date: Sun, 13 Aug 2017 15:21:43 +0200 Adjust jump targets so that the function implementation becomes smaller. * Move an error message so that it is present only once here. * Avoid another check for the local variable "rc" at the end. Signed-off-by: Markus Elfring --- security/selinux/ss/ebitmap.c | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/security/selinux/ss/ebitmap.c b/security/selinux/ss/ebitmap.c index ccf372db689c..03581d7ef817 100644 --- a/security/selinux/ss/ebitmap.c +++ b/security/selinux/ss/ebitmap.c @@ -350,21 +350,20 @@ int ebitmap_read(struct ebitmap *e, void *fp) __le32 buf[3]; int rc, i; - ebitmap_init(e); - rc = next_entry(buf, fp, sizeof buf); if (rc < 0) goto out; - mapunit = le32_to_cpu(buf[0]); + ebitmap_init(e); e->highbit = le32_to_cpu(buf[1]); count = le32_to_cpu(buf[2]); + mapunit = le32_to_cpu(buf[0]); if (mapunit != BITS_PER_U64) { printk(KERN_ERR "SELinux: ebitmap: map size %u does not " "match my size %zd (high bit was %d)\n", mapunit, BITS_PER_U64, e->highbit); - goto bad; + goto destroy_bitmap; } /* round up e->highbit */ @@ -377,27 +376,26 @@ int ebitmap_read(struct ebitmap *e, void *fp) } if (e->highbit && !count) - goto bad; + goto destroy_bitmap; for (i = 0; i < count; i++) { rc = next_entry(&startbit, fp, sizeof(u32)); - if (rc < 0) { - printk(KERN_ERR "SELinux: ebitmap: truncated map\n"); - goto bad; - } + if (rc) + goto report_truncated_map; + startbit = le32_to_cpu(startbit); if (startbit & (mapunit - 1)) { printk(KERN_ERR "SELinux: ebitmap start bit (%d) is " "not a multiple of the map unit size (%u)\n", startbit, mapunit); - goto bad; + goto destroy_bitmap; } if (startbit > e->highbit - mapunit) { printk(KERN_ERR "SELinux: ebitmap start bit (%d) is " "beyond the end of the bitmap (%u)\n", startbit, (e->highbit - mapunit)); - goto bad; + goto destroy_bitmap; } if (!n || startbit >= n->startbit + EBITMAP_SIZE) { @@ -407,7 +405,7 @@ int ebitmap_read(struct ebitmap *e, void *fp) printk(KERN_ERR "SELinux: ebitmap: out of memory\n"); rc = -ENOMEM; - goto bad; + goto destroy_bitmap; } /* round down */ tmp->startbit = startbit - (startbit % EBITMAP_SIZE); @@ -420,14 +418,13 @@ int ebitmap_read(struct ebitmap *e, void *fp) printk(KERN_ERR "SELinux: ebitmap: start bit %d" " comes after start bit %d\n", startbit, n->startbit); - goto bad; + goto destroy_bitmap; } rc = next_entry(&map, fp, sizeof(u64)); - if (rc < 0) { - printk(KERN_ERR "SELinux: ebitmap: truncated map\n"); - goto bad; - } + if (rc) + goto report_truncated_map; + map = le64_to_cpu(map); index = (startbit - n->startbit) / EBITMAP_UNIT_SIZE; @@ -438,9 +435,10 @@ int ebitmap_read(struct ebitmap *e, void *fp) } out: return rc; -bad: - if (!rc) - rc = -EINVAL; +report_truncated_map: + printk(KERN_ERR "SELinux: ebitmap: truncated map\n"); + rc = -EINVAL; +destroy_bitmap: ebitmap_destroy(e); goto out; }