From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-it0-f69.google.com (mail-it0-f69.google.com [209.85.214.69]) by kanga.kvack.org (Postfix) with ESMTP id 1C2306B02F3 for ; Tue, 27 Jun 2017 19:04:20 -0400 (EDT) Received: by mail-it0-f69.google.com with SMTP id r4so30344144ith.7 for ; Tue, 27 Jun 2017 16:04:20 -0700 (PDT) Received: from mail-it0-x232.google.com (mail-it0-x232.google.com. [2607:f8b0:4001:c0b::232]) by mx.google.com with ESMTPS id 89si480397iod.229.2017.06.27.16.04.19 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Jun 2017 16:04:19 -0700 (PDT) Received: by mail-it0-x232.google.com with SMTP id v202so21571099itb.0 for ; Tue, 27 Jun 2017 16:04:19 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <1497544976-7856-6-git-send-email-s.mesoraca16@gmail.com> References: <1497544976-7856-1-git-send-email-s.mesoraca16@gmail.com> <1497544976-7856-6-git-send-email-s.mesoraca16@gmail.com> From: Kees Cook Date: Tue, 27 Jun 2017 16:04:17 -0700 Message-ID: Subject: Re: [RFC v2 5/9] S.A.R.A. WX Protection Content-Type: text/plain; charset="UTF-8" Sender: owner-linux-mm@kvack.org List-ID: To: Salvatore Mesoraca Cc: LKML , linux-security-module , "kernel-hardening@lists.openwall.com" , Brad Spengler , PaX Team , Casey Schaufler , James Morris , "Serge E. Hallyn" , Linux-MM , "x86@kernel.org" , Jann Horn , Christoph Hellwig , Thomas Gleixner On Thu, Jun 15, 2017 at 9:42 AM, Salvatore Mesoraca wrote: > +static int sara_check_vmflags(vm_flags_t vm_flags) > +{ > + u16 sara_wxp_flags = get_current_sara_wxp_flags(); > + > + if (sara_enabled && wxprot_enabled) { > + if (sara_wxp_flags & SARA_WXP_WXORX && > + vm_flags & VM_WRITE && > + vm_flags & VM_EXEC) { > + if ((sara_wxp_flags & SARA_WXP_VERBOSE)) > + pr_wxp("W^X"); > + if (!(sara_wxp_flags & SARA_WXP_COMPLAIN)) > + return -EPERM; > + } > + if (sara_wxp_flags & SARA_WXP_MMAP && > + (vm_flags & VM_EXEC || > + (!(vm_flags & VM_MAYWRITE) && (vm_flags & VM_MAYEXEC))) && > + get_current_sara_mmap_blocked()) { > + if ((sara_wxp_flags & SARA_WXP_VERBOSE)) > + pr_wxp("executable mmap"); > + if (!(sara_wxp_flags & SARA_WXP_COMPLAIN)) > + return -EPERM; > + } > + } Given the subtle differences between these various if blocks (here and in the other hook), I think it would be nice to have some beefy comments here to describe specifically what's being checked (and why). It'll help others review this code, and help validate code against intent. I would also try to minimize the written code by creating a macro for a repeated pattern here: > + if ((sara_wxp_flags & SARA_WXP_VERBOSE)) > + pr_wxp("mprotect on file mmap"); > + if (!(sara_wxp_flags & SARA_WXP_COMPLAIN)) > + return -EACCES; These four lines are repeated several times with only the const char * and return value changing. Perhaps something like: #define sara_return(err, msg) do { \ if ((sara_wxp_flags & SARA_WXP_VERBOSE)) \ pr_wxp(err); \ if (!(sara_wxp_flags & SARA_WXP_COMPLAIN)) \ return -err; \ } while (0) Then each if block turns into something quite easier to parse: if (sara_wxp_flags & SARA_WXP_WXORX && vm_flags & VM_WRITE && vm_flags & VM_EXEC) sara_return(EPERM, "W^X"); -Kees -- Kees Cook Pixel Security -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org