From nobody Mon Sep 15 05:55:05 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B267BC3DA78 for ; Fri, 13 Jan 2023 21:35:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230108AbjAMVfx (ORCPT ); Fri, 13 Jan 2023 16:35:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230361AbjAMVfr (ORCPT ); Fri, 13 Jan 2023 16:35:47 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7617F033 for ; Fri, 13 Jan 2023 13:35:45 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id q10so2911597wrs.2 for ; Fri, 13 Jan 2023 13:35:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.huji.ac.il; s=mailhuji; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=AUN/ktfr557NsAgFfe15xaS8LcaziSyCzLEF8bg3s5M=; b=BIRAK53OfezEtN8URwkxVmK5kppDX4q8H2bA72tfN4L+0MiD/i6fPkbEX33sWodGUh dAz6/hc5downSGqBmpfb/UfyZc6qVW2rqslcGZ0rZnsvcOtDJwYMUmqXIBB4iKiJbewI mTmFX3o2MDXeQv6DuDGQJ/6okhKQvCwCaFQd4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AUN/ktfr557NsAgFfe15xaS8LcaziSyCzLEF8bg3s5M=; b=PwjuREpOJFneaw89JqVfzhDi5io/XmlPFGSQ98Fk/RADg2iXG6jdcgu2id/pj4AINj wXpJAjiMBkI5tulsJUAWnLNUqpdYc8czNXD0nHuK6RlSGu47zy3v0POQGKTU+kdF/dU8 8JmSQSJXlS+67ZtnCR35qDHtn2zX7Q7PbdTCsBSJWnjMl5SdTGlpkm58XFoQaA8w9Umz 59wnb/d+mhspVDkjpv2eqKnHwl9iWunBm9iazFkssiP3wQJKk+ey9dFx0sWgbqRpI+Ke P1LM/+liWygGLHA2qcEAqE0lhqTvBmYZ++BMFa+RHrPYrNw5aKnGb1BQgbuPZMU1wiyU O2dQ== X-Gm-Message-State: AFqh2krXoiXVb4DMc9rdzG+9DS8Ji+tqnC6YPGys9AHdmiDZB5Jr5SsP 46w7R1dAG3iLJdUndLls5rJaxYaWlAXcvCk3kcm7Va3h1KTiVeZ+fRUVHv7i5oRK049rQ+2De/T UbvPOZG/DkcDR2Lyj8NmDiLA7cypBNL2rWhwgWeDW5Je4V8ynD8dB6D+cWCa+hzPgFgso7UkPrC T5w65a9Ep5HRu6jv81Jd5ZJdY= X-Google-Smtp-Source: AMrXdXvE/8rU7gIEorncZU3DBDT4X3Eg5QqP/DvVq2f1hnYANi/szg8il3w3o9R1s/fOXCWai3RgUA== X-Received: by 2002:a05:6000:1707:b0:2ae:d9e2:7c80 with SMTP id n7-20020a056000170700b002aed9e27c80mr28262611wrc.8.1673645744176; Fri, 13 Jan 2023 13:35:44 -0800 (PST) Received: from MacBook-Pro-6.lan ([2a0d:6fc2:218c:1a00:2dd5:e78e:71c0:661b]) by smtp.gmail.com with ESMTPSA id n16-20020a5d4010000000b002bbed1388a5sm14508089wrp.15.2023.01.13.13.35.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:35:43 -0800 (PST) From: david.keisarschm@mail.huji.ac.il To: linux-kernel@vger.kernel.org, Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Jason@zx2c4.com, linux-mm@kvack.org, David Keisar Schmidt , aksecurity@gmail.com, ilay.bahat1@gmail.com Subject: [PATCH v4 1/3] slab_allocator: mm/slab.c: Replace invocation of weak PRNG Date: Fri, 13 Jan 2023 23:35:40 +0200 Message-Id: X-Mailer: git-send-email 2.38.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: David Keisar Schmidt The Slab allocator randomization uses the prandom_u32 PRNG. That was added to prevent attackers to obtain information on the heap state, by randomizing the freelists state. However, this PRNG turned out to be weak, as noted in commit c51f8f88d705 To fix it, we have changed the invocation of prandom_u32_state to get_rando= m_u32 to ensure the PRNG is strong. Since a modulo operation is applied right aft= er that, we used get_random_u32_below, to achieve uniformity. In addition, we changed the freelist_init_state union to struct, since the rnd_state inside which is used to store the state of prandom_u32, is not needed anymore, since get_random_u32 maintains its own state. Signed-off-by: David Keisar Schmidt --- Changes since v3: * edited commit message. Changes since v2: * replaced instances of get_random_u32 with get_random_u32_below in mm/slab.c. mm/slab.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/mm/slab.c b/mm/slab.c index 59c8e28f7..c259e0b09 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -2360,20 +2360,17 @@ static void cache_init_objs_debug(struct kmem_cache= *cachep, struct slab *slab) =20 #ifdef CONFIG_SLAB_FREELIST_RANDOM /* Hold information during a freelist initialization */ -union freelist_init_state { - struct { - unsigned int pos; - unsigned int *list; - unsigned int count; - }; - struct rnd_state rnd_state; +struct freelist_init_state { + unsigned int pos; + unsigned int *list; + unsigned int count; }; =20 /* * Initialize the state based on the randomization method available. * return true if the pre-computed list is available, false otherwise. */ -static bool freelist_state_initialize(union freelist_init_state *state, +static bool freelist_state_initialize(struct freelist_init_state *state, struct kmem_cache *cachep, unsigned int count) { @@ -2381,23 +2378,22 @@ static bool freelist_state_initialize(union freelis= t_init_state *state, unsigned int rand; =20 /* Use best entropy available to define a random shift */ - rand =3D get_random_u32(); + rand =3D get_random_u32_below(count); =20 /* Use a random state if the pre-computed list is not available */ if (!cachep->random_seq) { - prandom_seed_state(&state->rnd_state, rand); ret =3D false; } else { state->list =3D cachep->random_seq; state->count =3D count; - state->pos =3D rand % count; + state->pos =3D rand; ret =3D true; } return ret; } =20 /* Get the next entry on the list and randomize it using a random shift */ -static freelist_idx_t next_random_slot(union freelist_init_state *state) +static freelist_idx_t next_random_slot(struct freelist_init_state *state) { if (state->pos >=3D state->count) state->pos =3D 0; @@ -2418,7 +2414,7 @@ static void swap_free_obj(struct slab *slab, unsigned= int a, unsigned int b) static bool shuffle_freelist(struct kmem_cache *cachep, struct slab *slab) { unsigned int objfreelist =3D 0, i, rand, count =3D cachep->num; - union freelist_init_state state; + struct freelist_init_state state; bool precomputed; =20 if (count < 2) @@ -2447,8 +2443,7 @@ static bool shuffle_freelist(struct kmem_cache *cache= p, struct slab *slab) =20 /* Fisher-Yates shuffle */ for (i =3D count - 1; i > 0; i--) { - rand =3D prandom_u32_state(&state.rnd_state); - rand %=3D (i + 1); + rand =3D get_random_u32_below(i+1); swap_free_obj(slab, i, rand); } } else { --=20 2.38.0 From nobody Mon Sep 15 05:55:05 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E28CC6379F for ; Fri, 13 Jan 2023 21:37:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230187AbjAMVh6 (ORCPT ); Fri, 13 Jan 2023 16:37:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39456 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229759AbjAMVhx (ORCPT ); Fri, 13 Jan 2023 16:37:53 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD5CAE7D for ; Fri, 13 Jan 2023 13:37:50 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id j34-20020a05600c1c2200b003da1b054057so3920669wms.5 for ; Fri, 13 Jan 2023 13:37:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.huji.ac.il; s=mailhuji; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3TF4yl0qyemn80KbaOcWcMV7jh9ZuWBx78qicss9uQc=; b=p1ndSiVOwC15bI8PmJFG+sQFPFJP0t7Nb2kQN1/L1cRjs7817uXGxSoGRkPnYDtV6C S0DyVESqxTkJhavAAUaFaPVxERTTTYzOT/qD3O+CtZ+gHMZNTqKPpjvSOZm7IgSJAlzY iKLh98QZQTcQnkUG8so+tEhkdC0HTSTYZ3OvA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3TF4yl0qyemn80KbaOcWcMV7jh9ZuWBx78qicss9uQc=; b=Ij716rwiGSPjXvVASi6Tj1OvjfLtYnl2AfUg4kSMrRRoDs56yMie2rZe/2UziwtiYu nEJE3GM1ocTAVhXIBjAsNxsYuGiYiJ22XP82PO9wX7Mb2tGYnem1cXEAUOJ65R227glH TMoit8iM3wSgu9ViR3LzITUKMfXNDV7sXtrLpyT/dgypCW3V7qvICVIU9Wbbh2G9v9Jp 2Tbr1O6bMSBdKCMTj/Qi/1A7aOS/8VNHIjl07Xewy8S72L54KeXn0dMbbOELDtuXBHgA 0b4mQPHvhH14EB3YVRpmTxLj+5apdkpoJRwTbquP6fmM007hKo7lmRtXZzIyMtRuudJ6 hDIw== X-Gm-Message-State: AFqh2kpyfGa+EWll0p50FN4L0/tjAUsXKvI4hGs+snmBTuMpol0lgR6Q LwmwvFbKKt4rAR8SuR8kefZKXqiam+DQbeEeZ9Ktm/vuRPsVou7RR6vAfGxHbNo6KTjsoAtU496 AWQjm1cL3nP5fKyKuICZ9ExPjB0jT+v6P/uxuwlMOqjNVxxKbEqcDo893S23jPTcogoVIWaRBjm IEOjY4umfPUcZtG0UMXIWa2Sk= X-Google-Smtp-Source: AMrXdXtXqloDtFhx4OVf2TNQBeviRbt9VacFPJc7UOTi8ZcQ2RpkAcNb16kTYI3wvQIaO+jNjFrG9Q== X-Received: by 2002:a05:600c:3b14:b0:3cf:d18e:528b with SMTP id m20-20020a05600c3b1400b003cfd18e528bmr886119wms.39.1673645868866; Fri, 13 Jan 2023 13:37:48 -0800 (PST) Received: from MacBook-Pro-6.lan ([2a0d:6fc2:218c:1a00:2dd5:e78e:71c0:661b]) by smtp.gmail.com with ESMTPSA id o2-20020a05600c4fc200b003da2932bde0sm319944wmq.23.2023.01.13.13.37.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:37:48 -0800 (PST) From: david.keisarschm@mail.huji.ac.il To: linux-kernel@vger.kernel.org, Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Jason@zx2c4.com, linux-mm@kvack.org, David Keisar Schmidt , aksecurity@gmail.com, ilay.bahat1@gmail.com Subject: [PATCH v4 2/3] slab_allocator: mm/slab_common.c: Replace invocation of weak PRNG Date: Fri, 13 Jan 2023 23:37:45 +0200 Message-Id: X-Mailer: git-send-email 2.38.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: David Keisar Schmidt The Slab allocator randomization inside slab_common.c uses the prandom_u32 = PRNG. That was added to prevent attackers to obtain information on the heap state. However, this PRNG turned out to be weak, as noted in commit c51f8f88d705 To fix it, we have changed the invocation of prandom_u32_state to get_rando= m_u32 to ensure the PRNG is strong. Since a modulo operation is applied right after that, in the Fisher-Yates shuffle, we used get_random_u32_below, to achieve unifo= rmity. Signed-off-by: David Keisar Schmidt --- Changes since v3: * edited commit message. Changes since v2: * replaced instances of get_random_u32 with get_random_u32_below in mm/slab_common.c. mm/slab_common.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index 0042fb273..e254b2f55 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1130,7 +1130,7 @@ EXPORT_SYMBOL(kmalloc_large_node); =20 #ifdef CONFIG_SLAB_FREELIST_RANDOM /* Randomize a generic freelist */ -static void freelist_randomize(struct rnd_state *state, unsigned int *list, +static void freelist_randomize(unsigned int *list, unsigned int count) { unsigned int rand; @@ -1141,8 +1141,7 @@ static void freelist_randomize(struct rnd_state *stat= e, unsigned int *list, =20 /* Fisher-Yates shuffle */ for (i =3D count - 1; i > 0; i--) { - rand =3D prandom_u32_state(state); - rand %=3D (i + 1); + rand =3D get_random_u32_below(i+1); swap(list[i], list[rand]); } } @@ -1151,7 +1150,6 @@ static void freelist_randomize(struct rnd_state *stat= e, unsigned int *list, int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count, gfp_t gfp) { - struct rnd_state state; =20 if (count < 2 || cachep->random_seq) return 0; @@ -1160,10 +1158,7 @@ int cache_random_seq_create(struct kmem_cache *cache= p, unsigned int count, if (!cachep->random_seq) return -ENOMEM; =20 - /* Get best entropy at this stage of boot */ - prandom_seed_state(&state, get_random_long()); - - freelist_randomize(&state, cachep->random_seq, count); + freelist_randomize(cachep->random_seq, count); return 0; } =20 --=20 2.38.0 From nobody Mon Sep 15 05:55:05 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E837AC6379F for ; Fri, 13 Jan 2023 21:39:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230183AbjAMVjz (ORCPT ); Fri, 13 Jan 2023 16:39:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229825AbjAMVjv (ORCPT ); Fri, 13 Jan 2023 16:39:51 -0500 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88E78EA4 for ; Fri, 13 Jan 2023 13:39:50 -0800 (PST) Received: by mail-wr1-x429.google.com with SMTP id b5so1073957wrn.0 for ; Fri, 13 Jan 2023 13:39:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.huji.ac.il; s=mailhuji; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=q3T8MxbgSdvCZWZbC6/MDpVm95aqJIIRd85jHr1PonM=; b=M6gDdpFiozHZk80GaW26bXTAk9Cw+PjbT+1K44PhKPgz/bEnK/uNKhvcPCG9L1U8HY XfepmZRwdcBJ/ug2WLCC2zxXG9PwUKOxXDW/t3lLbn4sM8TbE1J+GWpgWSun9Wnlr4cZ iCORNnOVmiDF0S4tMbIQ58FNvEeQPTtVFVbEk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=q3T8MxbgSdvCZWZbC6/MDpVm95aqJIIRd85jHr1PonM=; b=u5/PvUV89skD0fPRMwzvH1vNwBIq1A64uOWpAXJeJHY1MjjUuhtvA9Sp/0RL5drBxm CvdG1n2y14M6Sf2rIksEWlYSxAMgop7aS1qbbmWAGXw4nVA523ix7AbArA9oVOx3k0E2 rxNmkOJAixfdB400gMqzCc6W5AdCrU/MTlMEGwiRjuu3npd/WwXNFJMIjAl2ThkVUJFI 5jc/fg490l/K1OOvNyvQQAxjzyOu431lPAwkuyNu8SQsw26CLp+ragZKpca1G8msSRuW iw8XWDVwz8yrXXNvjfXzzKdrWdJmQ9OWvdGbm/RDhT4xX7glEkTATjyh/+0Gm23/fpJa VkXA== X-Gm-Message-State: AFqh2koeUz/M/9XzN3eSoMSc2QRw1pJkiv7lwwEz6HgQa6XAArdx3T5u qrlQRrTfvzuigo+2YX8Z8S+CiFOg6VUOvXXVH+gWL4fruSUKp4X113Lp/YAA+OXLTAKl8kLgF4l CnXV+3jS+VoE9+hW4OiJkWSguaU/I6BwA04nh7tyMEZfqVLMB90CTVFT+hzgZKHHfdsHz43LTba 5juupUqz0UJMDHPL7AC3wORsc= X-Google-Smtp-Source: AMrXdXvlMI4DsJ8R492xfP3fN5zBO8tes6MxNy/Bct75Vb+fJ5GFozm3v4fx7gR/DclIAAQNNhFnRA== X-Received: by 2002:a5d:56c1:0:b0:2ba:cd91:5ae7 with SMTP id m1-20020a5d56c1000000b002bacd915ae7mr17275676wrw.47.1673645988608; Fri, 13 Jan 2023 13:39:48 -0800 (PST) Received: from MacBook-Pro-6.lan ([2a0d:6fc2:218c:1a00:2dd5:e78e:71c0:661b]) by smtp.gmail.com with ESMTPSA id e7-20020a056000120700b00241dd5de644sm19841461wrx.97.2023.01.13.13.39.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 13:39:48 -0800 (PST) From: david.keisarschm@mail.huji.ac.il To: linux-kernel@vger.kernel.org, Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: Jason@zx2c4.com, keescook@chromium.org, David Keisar Schmidt , aksecurity@gmail.com, ilay.bahat1@gmail.com Subject: [PATCH v4 3/3] x86 mm, x86 architecture (32-bit and 64-bit): arch/x86/mm/kaslr.c: Adds 64bits version of prandom_seed_state Date: Fri, 13 Jan 2023 23:39:45 +0200 Message-Id: <295dceed7bef2391f86e751b9bfc9a34347062e4.1673470326.git.david.keisarschm@mail.huji.ac.il> X-Mailer: git-send-email 2.38.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: David Keisar Schmidt The memory randomization of the virtual address space of kernel memory regi= ons (physical memory mapping, vmalloc & vmemmap) inside arch/x86/mm/kaslr.c is based on the function prandom_bytes_state which uses the prandom_u32 PRN= G. However, the seeding here is done by calling prandom_seed_state, which effectively uses only 32bits of the seed, which means that observing = ONE region's offset (say 30 bits) can provide the attacker with 2 possible seeds (from which the attacker can calculate the remaining two regions) Hence, we implemented an adjusted version of prandom_seed_state, inside kas= lr.c so it takes advantage of all the seed's 64 bits. With this implementation, enumerating over the seed is quite unfeasible, and attacking the linearity = requires ~113 bits which we don't get with two exposed region offsets (but rather up to 30 bits each). Signed-off-by: David Keisar Schmidt --- Changes since v3: * We took a different approach, and replaced the invocation of prandom_bytes_state, to a revised version which is more secure. Changes since v2: * edited commit message. arch/x86/mm/kaslr.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c index 557f0fe25..5fd73d5ad 100644 --- a/arch/x86/mm/kaslr.c +++ b/arch/x86/mm/kaslr.c @@ -59,6 +59,29 @@ static inline unsigned long get_padding(struct kaslr_mem= ory_region *region) { return (region->size_tb << TB_SHIFT); } +static inline void _kaslr_prandom_seed_state(struct rnd_state *state, u64 = seed) +{ + u32 i =3D ((seed >> 32) ^ (seed << 10) ^ seed) & 0xffffffffUL; + // To take advantage of all 64 bits of the seed + u32 j =3D ((seed>>32) ^ (seed<<10)) & 0xffffffffUL; + state->s1 =3D __seed(i, 2U); + state->s2 =3D __seed(j, 8U); + /* Ensure no obvious linear relation with the previous states */ + state->s3 =3D __seed(next_pseudo_random32(i+j), 16U); + state->s4 =3D __seed(next_pseudo_random32(j-((i>>16)^(i<<16))), 128U); + + /* Calling RNG ten times to satisfy recurrence condition */ + prandom_u32_state(state); + prandom_u32_state(state); + prandom_u32_state(state); + prandom_u32_state(state); + prandom_u32_state(state); + prandom_u32_state(state); + prandom_u32_state(state); + prandom_u32_state(state); + prandom_u32_state(state); + prandom_u32_state(state); +} =20 /* Initialize base and padding for each memory region randomized with KASL= R */ void __init kernel_randomize_memory(void) @@ -113,7 +136,8 @@ void __init kernel_randomize_memory(void) for (i =3D 0; i < ARRAY_SIZE(kaslr_regions); i++) remain_entropy -=3D get_padding(&kaslr_regions[i]); =20 - prandom_seed_state(&rand_state, kaslr_get_random_long("Memory")); + _kaslr_prandom_seed_state(&rand_state, kaslr_get_random_long + ("Memory")); =20 for (i =3D 0; i < ARRAY_SIZE(kaslr_regions); i++) { unsigned long entropy; --=20 2.38.0