From patchwork Mon Sep 11 19:50:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 9948029 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 2AE0D602C9 for ; Mon, 11 Sep 2017 19:53:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 232CD28D34 for ; Mon, 11 Sep 2017 19:53:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1694728D36; Mon, 11 Sep 2017 19:53: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=-4.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, GAPPY_SUBJECT, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=unavailable 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 963C628D34 for ; Mon, 11 Sep 2017 19:53:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751507AbdIKTxi (ORCPT ); Mon, 11 Sep 2017 15:53:38 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:35439 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751423AbdIKTvo (ORCPT ); Mon, 11 Sep 2017 15:51:44 -0400 Received: by mail-wm0-f67.google.com with SMTP id e64so7713084wmi.2; Mon, 11 Sep 2017 12:51:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=WKT/gIuTDhVsDIICu53YNf+0Ja+FcVsQY4rzwzzhJd4=; b=h/d37Ec3X1EbN/meare17wSt1dJCBruI01SPUqaONm9RI0StQY4eTrio4t/6NbR3lK wadKCLzB4U+pasERsWKU92TIy4443MnK3CeuL7jfj/Lttt/5FMzDVZhfzf73lbNKxpRW pTpQ5h1qQQCWXs9gIwCoVxuuGq/06vMwYX7R5Or87MD+wYSYusqpNwmKNfr0XfvzwPHR KNGYdxO4YiEu1zi00CkXxBymzqIXfBjeRGtUcvqkcLejzynETwy1VWrWHEPgwXOakgvU XS3ic9mp4Sxjk7DDKPnJrXmEzq2uszXKe05i/9D5Ofs5x1U4jIWsuNeU7XpqAUrz8W5v hYfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=WKT/gIuTDhVsDIICu53YNf+0Ja+FcVsQY4rzwzzhJd4=; b=ru85rixa3eSB2YIK2FYdk5oaMSMZ9IeQsZy1KTL4mzOLMQKgiOb/JDpqd2jDvMOvif gFompWB0VoD9U3T+kUqaKX+OtYngXJ1FJOaSLCAKqdzjZu8IndChMqHWpecX9LMcvBu8 6U+vMIwUrZxNGkAOWF4srCtDs1Wf/NO+JW0du0GA0mvSxVn8I1037TZ1c2WNPbpz12CF O/+FxfW+C+hth7UQWlqmtTGvvGldmU1qCwyICgCqo4KkNYlF9Ar3kxMa9f4xqh/UeHpk 14uSKIl0CK2ElEYax2hVQ1hpJx329m2eJakQX0ERhXsAMmFZyoYGe+xe7ZfrAa+h0K70 zZYg== X-Gm-Message-State: AHPjjUjYZISIy0J7yiBBOgZQdgFs1DWFABZGjABB6AR3Rkg1WNpZz1PR OUIjntwlhWPkupQDEa+HuuiwLXNfczg= X-Google-Smtp-Source: AOwi7QA8GSApwL8LfKzAz1CL2JVcFwhu5+FNOQi4n3EpqD2ECWDuR8czNc9M/CbKuM2I5QpvwzFl0g== X-Received: by 10.28.70.133 with SMTP id t127mr947280wma.42.1505159502843; Mon, 11 Sep 2017 12:51:42 -0700 (PDT) Received: from localhost ([93.66.104.212]) by smtp.gmail.com with ESMTPSA id a39sm14663448wrc.48.2017.09.11.12.51.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 11 Sep 2017 12:51:42 -0700 (PDT) From: Salvatore Mesoraca To: linux-kernel@vger.kernel.org Cc: linux-security-module@vger.kernel.org, kernel-hardening@lists.openwall.com, Salvatore Mesoraca , Brad Spengler , PaX Team , Casey Schaufler , Kees Cook , James Morris , "Serge E. Hallyn" Subject: [RFC v3 4/9] S.A.R.A. cred blob management Date: Mon, 11 Sep 2017 21:50:22 +0200 Message-Id: <1505159427-11747-5-git-send-email-s.mesoraca16@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1505159427-11747-1-git-send-email-s.mesoraca16@gmail.com> References: <1505159427-11747-1-git-send-email-s.mesoraca16@gmail.com> Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Creation of the S.A.R.A. cred blob management "API". In order to allow S.A.R.A. to be stackable with other LSMs, it doesn't use the "security" field of struct cred, instead it uses an ad hoc field named security_sara. This solution is probably not acceptable for upstream, so this part will be modified as soon as the LSM stackable cred blob management will be available. Signed-off-by: Salvatore Mesoraca --- include/linux/cred.h | 3 ++ security/sara/Makefile | 2 +- security/sara/include/sara_data.h | 47 +++++++++++++++++++++++ security/sara/main.c | 6 +++ security/sara/sara_data.c | 79 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 security/sara/include/sara_data.h create mode 100644 security/sara/sara_data.c diff --git a/include/linux/cred.h b/include/linux/cred.h index 099058e..b65b666 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -141,6 +141,9 @@ struct cred { #ifdef CONFIG_SECURITY void *security; /* subjective LSM security */ #endif +#ifdef CONFIG_SECURITY_SARA + void *security_sara; +#endif struct user_struct *user; /* real user ID subscription */ struct user_namespace *user_ns; /* user_ns the caps and keyrings are relative to. */ struct group_info *group_info; /* supplementary groups for euid/fsgid */ diff --git a/security/sara/Makefile b/security/sara/Makefile index 8acd291..14bf7a8 100644 --- a/security/sara/Makefile +++ b/security/sara/Makefile @@ -1,3 +1,3 @@ obj-$(CONFIG_SECURITY_SARA) := sara.o -sara-y := main.o securityfs.o utils.o +sara-y := main.o securityfs.o utils.o sara_data.o diff --git a/security/sara/include/sara_data.h b/security/sara/include/sara_data.h new file mode 100644 index 0000000..7ed04fd --- /dev/null +++ b/security/sara/include/sara_data.h @@ -0,0 +1,47 @@ +/* + * S.A.R.A. Linux Security Module + * + * Copyright (C) 2017 Salvatore Mesoraca + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, as + * published by the Free Software Foundation. + * + */ + +#ifndef __SARA_DATA_H +#define __SARA_DATA_H + +#include + +int sara_data_init(void) __init; + +#ifdef CONFIG_SECURITY_SARA_WXPROT + +struct sara_data { + unsigned long relro_page; + u16 wxp_flags; + bool relro_page_found; + bool mmap_blocked; +}; + +#define get_sara_data_leftvalue(X) ((X)->security_sara) +#define get_sara_data(X) ((struct sara_data *) (X)->security_sara) +#define get_current_sara_data() get_sara_data(current_cred()) + +#define get_sara_wxp_flags(X) (get_sara_data((X))->wxp_flags) +#define get_current_sara_wxp_flags() get_sara_wxp_flags(current_cred()) + +#define get_sara_relro_page(X) (get_sara_data((X))->relro_page) +#define get_current_sara_relro_page() get_sara_relro_page(current_cred()) + +#define get_sara_relro_page_found(X) (get_sara_data((X))->relro_page_found) +#define get_current_sara_relro_page_found() \ + get_sara_relro_page_found(current_cred()) + +#define get_sara_mmap_blocked(X) (get_sara_data((X))->mmap_blocked) +#define get_current_sara_mmap_blocked() get_sara_mmap_blocked(current_cred()) + +#endif + +#endif /* __SARA_H */ diff --git a/security/sara/main.c b/security/sara/main.c index aaddd32..0fc1761 100644 --- a/security/sara/main.c +++ b/security/sara/main.c @@ -15,6 +15,7 @@ #include #include "include/sara.h" +#include "include/sara_data.h" #include "include/securityfs.h" static const int sara_version = SARA_VERSION; @@ -90,6 +91,11 @@ void __init sara_init(void) goto error; } + if (sara_data_init()) { + pr_crit("impossible to initialize creds.\n"); + goto error; + } + pr_debug("initialized.\n"); if (sara_enabled) diff --git a/security/sara/sara_data.c b/security/sara/sara_data.c new file mode 100644 index 0000000..8f11cd1 --- /dev/null +++ b/security/sara/sara_data.c @@ -0,0 +1,79 @@ +/* + * S.A.R.A. Linux Security Module + * + * Copyright (C) 2017 Salvatore Mesoraca + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, as + * published by the Free Software Foundation. + * + */ + +#include "include/sara_data.h" + +#ifdef CONFIG_SECURITY_SARA_WXPROT +#include +#include +#include + +static int sara_cred_alloc_blank(struct cred *cred, gfp_t gfp) +{ + struct sara_data *d; + + d = kzalloc(sizeof(*d), gfp); + if (d == NULL) + return -ENOMEM; + get_sara_data_leftvalue(cred) = d; + return 0; +} + +static void sara_cred_free(struct cred *cred) +{ + struct sara_data *d; + + d = get_sara_data(cred); + if (d != NULL) { + kfree(d); + get_sara_data_leftvalue(cred) = NULL; + } +} + +static int sara_cred_prepare(struct cred *new, const struct cred *old, + gfp_t gfp) +{ + struct sara_data *d; + + d = kmemdup(get_sara_data(old), sizeof(*d), gfp); + if (d == NULL) + return -ENOMEM; + get_sara_data_leftvalue(new) = d; + return 0; +} + +static void sara_cred_transfer(struct cred *new, const struct cred *old) +{ + *get_sara_data(new) = *get_sara_data(old); +} + +static struct security_hook_list data_hooks[] __ro_after_init = { + LSM_HOOK_INIT(cred_alloc_blank, sara_cred_alloc_blank), + LSM_HOOK_INIT(cred_free, sara_cred_free), + LSM_HOOK_INIT(cred_prepare, sara_cred_prepare), + LSM_HOOK_INIT(cred_transfer, sara_cred_transfer), +}; + +int __init sara_data_init(void) +{ + security_add_hooks(data_hooks, ARRAY_SIZE(data_hooks), "sara"); + return sara_cred_alloc_blank((struct cred *) current->real_cred, + GFP_KERNEL); +} + +#else /* CONFIG_SECURITY_SARA_WXPROT */ + +int __init sara_data_init(void) +{ + return 0; +} + +#endif