From patchwork Wed Mar 29 18:16:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9652237 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 8D963602C8 for ; Wed, 29 Mar 2017 18:19:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7713928510 for ; Wed, 29 Mar 2017 18:19:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6BF682851A; Wed, 29 Mar 2017 18:19: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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 17A8828517 for ; Wed, 29 Mar 2017 18:19:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=7lEHQ/fHQk05oRzIGQpno5qGuQdzcFxDx+S06WC1K5g=; b=hsYEF69T/dgRlaoHIgQ35PVV1b KZKhFdc92kwQXGwXxJTwjvJFdeJ2CcgHcvTvofpGNFdxR2ZN3R7KVC6iA9w6OjamRGJcQIr6alw3h hamHf9famKXVL4VOdX3KtuNOQXgXFMH+xak8HJo0EX0K9rMe6svkU1yR5GTNz0XLdGSwTBU1CdLRb YthM9tb98ru70AVWonz0DLx/CT+fKvPChbu3Ey73JQ3AuUfWZ0vBSmhZaE3jjcifDe0/8TIhdy6gb +oZ5Q6O/SymWZo3U8QJhHHJJg9andfzftIv41voACok5GncJn6eILKtfrbH9ClZqtu4jJjFf7Ust/ AjgPUGpw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1ctIBO-0007bk-Ut; Wed, 29 Mar 2017 18:18:58 +0000 Received: from mail-pg0-x22a.google.com ([2607:f8b0:400e:c05::22a]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1ctI9F-00058R-Cr for linux-arm-kernel@lists.infradead.org; Wed, 29 Mar 2017 18:16:52 +0000 Received: by mail-pg0-x22a.google.com with SMTP id 81so14646705pgh.2 for ; Wed, 29 Mar 2017 11:16:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=qdj1PgfoQ6KIQ3ryIEYeJaWEudzrnKF504GPeo9Z4f0=; b=heB/X+8HS1MzzQVoLZT+0eYddrtC3dUCHCjB78Q6sgCjC74sfVb2FKm/EpW16h7NN9 5LUX7NV3zloMAWp8Q3xZS2s1VMb2gJBM5uha9A7uuQ410ZMBBjc60stcJKddO2onDLsi vz5rJ6S3rbS4GQzdO5W3Mnw7fHgmsTnOhvU6c= 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=qdj1PgfoQ6KIQ3ryIEYeJaWEudzrnKF504GPeo9Z4f0=; b=N8ull4tZlQhNy/euwVs6EB1HqeEpxlNmJFAe/P/6dBjz4EKqflcODTxbFBXxz5O1X4 +jpryvaZgkUO8yIwWoEJg7XUb2Bv5rjMMOal0CBGo5fNhQScY1tzbJL5+1bHf3O6g1+F ovj/vdPeOKAXTrifd9N3UsztqkEMxh9PhCzbNyhzcB6gOpwOWaYiaz3+tcEtRDlG/2D6 yiXg/5rGPHLmWcS0jHK88JZ4O972tNY+KQ1uGPZiSY/B8IdrgTVXYXEHYoKhd71U+nXV gDQe4OfUEgZXm/I0YoYXzuC/UFvB1e+Np4u9V+3azYOdsugzGEbfx+85in8fhod1XYpe VUaQ== X-Gm-Message-State: AFeK/H3l0dlvtr48hgtZss7DPj9YcXUl8doU4kEppH6SsOJcicPcMrQAbDYEr63YJnMuNaYD X-Received: by 10.84.241.71 with SMTP id u7mr2126805plm.104.1490811386627; Wed, 29 Mar 2017 11:16:26 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id f3sm14840420pga.34.2017.03.29.11.16.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Mar 2017 11:16:25 -0700 (PDT) From: Kees Cook To: kernel-hardening@lists.openwall.com Subject: [RFC v2][PATCH 11/11] cgroups: force all struct cftype const Date: Wed, 29 Mar 2017 11:16:03 -0700 Message-Id: <1490811363-93944-12-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1490811363-93944-1-git-send-email-keescook@chromium.org> References: <1490811363-93944-1-git-send-email-keescook@chromium.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170329_111645_645747_8557FEC4 X-CRM114-Status: GOOD ( 13.79 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Hoeun Ryu , Kees Cook , x86@kernel.org, Russell King , linux-kernel@vger.kernel.org, Emese Revfy , Andy Lutomirski , PaX Team , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP As found in PaX, mark struct cftype with __do_const and add helpers to deal with rare writes. This is a more complex example of a write-rarely structure, which needs to use list helpers and blocks of begin/end pairs to perform the needed updates. With this change and the constify plugin enabled, the before/after section byte sizes show: before: rodata: 0x2cc2f0 data: 0x130d00 after: rodata: 0x2cf2f0 (+74478) data: 0x12e5c0 (-65710) Signed-off-by: Kees Cook --- include/linux/cgroup-defs.h | 2 +- kernel/cgroup/cgroup.c | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index 6a3f850cabab..67563a80d01f 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -434,7 +434,7 @@ struct cftype { #ifdef CONFIG_DEBUG_LOCK_ALLOC struct lock_class_key lockdep_key; #endif -}; +} __do_const; /* * Control Group subsystem type. diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 48851327a15e..94188df45f96 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -3058,11 +3058,11 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, int ret; #ifdef CONFIG_DEBUG_LOCK_ALLOC - key = &cft->lockdep_key; + key = (struct lock_class_key *)&cft->lockdep_key; #endif kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), - cgroup_file_mode(cft), 0, cft->kf_ops, cft, - NULL, key); + cgroup_file_mode(cft), 0, cft->kf_ops, + (void *)cft, NULL, key); if (IS_ERR(kn)) return PTR_ERR(kn); @@ -3165,11 +3165,16 @@ static void cgroup_exit_cftypes(struct cftype *cfts) /* free copy for custom atomic_write_len, see init_cftypes() */ if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) kfree(cft->kf_ops); - cft->kf_ops = NULL; - cft->ss = NULL; + + rare_write_begin(); + __rare_write(cft->kf_ops, NULL); + __rare_write(cft->ss, NULL); /* revert flags set by cgroup core while adding @cfts */ - cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL); + __rare_write(cft->flags, + cft->flags & ~(__CFTYPE_ONLY_ON_DFL | + __CFTYPE_NOT_ON_DFL)); + rare_write_end(); } } @@ -3200,8 +3205,10 @@ static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) kf_ops->atomic_write_len = cft->max_write_len; } - cft->kf_ops = kf_ops; - cft->ss = ss; + rare_write_begin(); + __rare_write(cft->kf_ops, kf_ops); + __rare_write(cft->ss, ss); + rare_write_end(); } return 0; @@ -3214,7 +3221,7 @@ static int cgroup_rm_cftypes_locked(struct cftype *cfts) if (!cfts || !cfts[0].ss) return -ENOENT; - list_del(&cfts->node); + rare_list_del(&cfts->node); cgroup_apply_cftypes(cfts, false); cgroup_exit_cftypes(cfts); return 0; @@ -3271,7 +3278,7 @@ static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) mutex_lock(&cgroup_mutex); - list_add_tail(&cfts->node, &ss->cfts); + rare_list_add_tail(&cfts->node, &ss->cfts); ret = cgroup_apply_cftypes(cfts, true); if (ret) cgroup_rm_cftypes_locked(cfts); @@ -3292,8 +3299,10 @@ int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) { struct cftype *cft; + rare_write_begin(); for (cft = cfts; cft && cft->name[0] != '\0'; cft++) - cft->flags |= __CFTYPE_ONLY_ON_DFL; + __rare_write(cft->flags, cft->flags | __CFTYPE_ONLY_ON_DFL); + rare_write_end(); return cgroup_add_cftypes(ss, cfts); } @@ -3309,8 +3318,10 @@ int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) { struct cftype *cft; + rare_write_begin(); for (cft = cfts; cft && cft->name[0] != '\0'; cft++) - cft->flags |= __CFTYPE_NOT_ON_DFL; + __rare_write(cft->flags, cft->flags | __CFTYPE_NOT_ON_DFL); + rare_write_end(); return cgroup_add_cftypes(ss, cfts); }