Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1112336imm; Fri, 8 Jun 2018 10:11:05 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKS5hURfbEOOlJxEbYuK3rTs/75D9AAZVIOvls+gi1fEkzXw9kf9W3vIzGw5LMIn01BDK5Q X-Received: by 2002:a63:7e51:: with SMTP id o17-v6mr5827508pgn.398.1528477865696; Fri, 08 Jun 2018 10:11:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528477865; cv=none; d=google.com; s=arc-20160816; b=R117AP8IRl85kwc+45Ossvx1boISSrTslDjCfNWvKrDp2lnQobC7CNLDSOJKsJwdnm Q5lLWmvKOw+z5x9hyu8xHH9xod4n2eh2NsvvT5r9PpAg1oL6Pr4J2ZPutCAtr0wL4/cj ejtfRbTqfc65Krp9JRyued99CVV0tYa58Hpbx0OwXgN6ag13hLknpWURwsEid91RK8Jr ZwUoQN9NFsLoFTKNhwjmhajHnWQF6LEblEeR12kTzseCqHo7PbVDE4GoieYnZNuJKhuG 3ycak2WKTN8yY8MJxySWkXXbBJbd3iPOAV4EgRtT+MwpFy+he9eZ9llSiHLcT9ffdVD3 vPBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature :arc-authentication-results; bh=pdcuS6LtC8qdLf8L60bKm6PUeKDjJHtPmEoxcZtVvBE=; b=e+hxz76pdgsQhcscc0smG10XY5kth5kjmGzjxKonGMvzr5HkgWgl9dxyIj7FqRRRtG FjXq0qR/i6lOJPO4AKpHVpCL2mSrbUO2kVQHhA9Rwj5aWqCjI7q2igsWMuPke+ImhP12 fswYnrXvzUyCcqGCRgrLwcz+uxfayzg/O+c8BSD2h4fL/aiS3actjJKTIayzdZu+24K8 OvW1U5/UWe4ayMLyun4i0/ip4Csv0FSvgq4fwJtZqxo6FYvsN22MRCSHJ4bdzoxiem3e 0TQDns3yCa7jtaVBwvTgKgb4obc1l5jf09iCFhmdD2k0+e/HySf4r9P8qrHoo8lVyats ENQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=B4t3Ndbp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t9-v6si18631945pgn.559.2018.06.08.10.10.51; Fri, 08 Jun 2018 10:11:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=B4t3Ndbp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752682AbeFHRJ3 (ORCPT + 99 others); Fri, 8 Jun 2018 13:09:29 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:55196 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751286AbeFHRJ2 (ORCPT ); Fri, 8 Jun 2018 13:09:28 -0400 Received: by mail-wm0-f67.google.com with SMTP id o13-v6so4511195wmf.4 for ; Fri, 08 Jun 2018 10:09:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=pdcuS6LtC8qdLf8L60bKm6PUeKDjJHtPmEoxcZtVvBE=; b=B4t3NdbpTgL+4Emq5eau/ZF3sUPt0MnkQIKRA7JT0Vy9eo929WgnCdIfeGvyKRMSlw Xt4Wbzq4WfOlXwhuKWtMZLNDFCJm5bNDx1u35dI9GdaUMspRJS3VIANHXNSd0IhjW9x9 FMF4e7a1UcBwRK32lLyENOAlkOnqkO+dgrFj4wX26hdPe2JuWhX13IswEK1jqoOd4hYB QFxe1xGMa7fu9qnMBklPHrN/4H50aN4056+r4SfDJzAYHMUUXOk7JgNqv5dVBwD0ETwi ZapiagyhQ3F1Ku3+YWfgOx9ghOD/otiMNtgWRfviqXCJHY4ZkQTW5uDeOFgNZZ4Yy3Dt vGyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=pdcuS6LtC8qdLf8L60bKm6PUeKDjJHtPmEoxcZtVvBE=; b=Kf/WhJ7Iah7hauzcETxqe/wWsYi3euk6Baxyf0KikeXTvfeJmZKO/D5eVfECd8rOcL cwQ0zHqzuYorYCQ81Zpvw7eoVAJwLAkyZLNO1iSlrDQvmC8/5z7JRmLib8fub5QD+v9p RBOPf6YraWDB/cxBpT9nwwOEwAOAXupY1XudWnD9ySmE3vCrT3EZtjSgnjJZpdhLidaz TtgGkzy7/ozU8HnCU0+xSHODpoIE23pWvyhCI1Bhhyh0FWDKf2yom0bqRSy5VaJI2zN9 C4kVJKxQQ0/Omr5xA6sG941w221EtwX0RZHHVWJH4+diBjHHGheuKQE6lxONdIrwHkvl K8lQ== X-Gm-Message-State: APt69E1o5wwlkDb+b8fe0hxAPzPg1j60OVXSaknYMXBtZ8dc34Izz7xh x30Z2uZWAokdoRSohh+cVnQ4gZJDBV3sK1Bublm32A== X-Received: by 2002:a1c:1c8f:: with SMTP id c137-v6mr2091516wmc.142.1528477766923; Fri, 08 Jun 2018 10:09:26 -0700 (PDT) MIME-Version: 1.0 References: <20180608170607.29120-1-guro@fb.com> In-Reply-To: <20180608170607.29120-1-guro@fb.com> From: Shakeel Butt Date: Fri, 8 Jun 2018 10:09:14 -0700 Message-ID: Subject: Re: [PATCH] mm: fix null pointer dereference in mem_cgroup_protected To: Roman Gushchin Cc: Andrew Morton , Linux MM , kernel-team@fb.com, LKML , Johannes Weiner , Michal Hocko Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 8, 2018 at 10:06 AM Roman Gushchin wrote: > > Shakeel reported a crash in mem_cgroup_protected(), which > can be triggered by memcg reclaim if the legacy cgroup v1 > use_hierarchy=0 mode is used: > > [ 226.060572] BUG: unable to handle kernel NULL pointer dereference > at 0000000000000120 > [ 226.068310] PGD 8000001ff55da067 P4D 8000001ff55da067 PUD 1fdc7df067 PMD 0 > [ 226.075191] Oops: 0000 [#4] SMP PTI > [ 226.078637] CPU: 0 PID: 15581 Comm: bash Tainted: G D > 4.17.0-smp-clean #5 > [ 226.086635] Hardware name: ... > [ 226.094546] RIP: 0010:mem_cgroup_protected+0x54/0x130 > [ 226.099533] Code: 4c 8b 8e 00 01 00 00 4c 8b 86 08 01 00 00 48 8d > 8a 08 ff ff ff 48 85 d2 ba 00 00 00 00 48 0f 44 ca 48 39 c8 0f 84 cf > 00 00 00 <48> 8b 81 20 01 00 00 4d 89 ca 4c 39 c8 4c 0f 46 d0 4d 85 d2 > 74 05 > [ 226.118194] RSP: 0000:ffffabe64dfafa58 EFLAGS: 00010286 > [ 226.123358] RAX: ffff9fb6ff03d000 RBX: ffff9fb6f5b1b000 RCX: 0000000000000000 > [ 226.130406] RDX: 0000000000000000 RSI: ffff9fb6f5b1b000 RDI: ffff9fb6f5b1b000 > [ 226.137454] RBP: ffffabe64dfafb08 R08: 0000000000000000 R09: 0000000000000000 > [ 226.144503] R10: 0000000000000000 R11: 000000000000c800 R12: ffffabe64dfafb88 > [ 226.151551] R13: ffff9fb6f5b1b000 R14: ffffabe64dfafb88 R15: ffff9fb77fffe000 > [ 226.158602] FS: 00007fed1f8ac700(0000) GS:ffff9fb6ff400000(0000) > knlGS:0000000000000000 > [ 226.166594] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > [ 226.172270] CR2: 0000000000000120 CR3: 0000001fdcf86003 CR4: 00000000001606f0 > [ 226.179317] Call Trace: > [ 226.181732] ? shrink_node+0x194/0x510 > [ 226.185435] do_try_to_free_pages+0xfd/0x390 > [ 226.189653] try_to_free_mem_cgroup_pages+0x123/0x210 > [ 226.194643] try_charge+0x19e/0x700 > [ 226.198088] mem_cgroup_try_charge+0x10b/0x1a0 > [ 226.202478] wp_page_copy+0x134/0x5b0 > [ 226.206094] do_wp_page+0x90/0x460 > [ 226.209453] __handle_mm_fault+0x8e3/0xf30 > [ 226.213498] handle_mm_fault+0xfe/0x220 > [ 226.217285] __do_page_fault+0x262/0x500 > [ 226.221158] do_page_fault+0x28/0xd0 > [ 226.224689] ? page_fault+0x8/0x30 > [ 226.228048] page_fault+0x1e/0x30 > [ 226.231323] RIP: 0033:0x485b72 > > The problem happens because parent_mem_cgroup() returns a NULL > pointer, which is dereferenced later without a check. > > As cgroup v1 has no memory guarantee support, let's make > mem_cgroup_protected() immediately return MEMCG_PROT_NONE, > if the given cgroup has no parent (non-hierarchical mode is used). > > Reported-by: Shakeel Butt > Signed-off-by: Roman Gushchin > Cc: Johannes Weiner > Cc: Michal Hocko > Cc: Andrew Morton Tested-by: Shakeel Butt > --- > mm/memcontrol.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 6c9fb4e47be3..6205ba512928 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -5750,6 +5750,9 @@ enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root, > elow = memcg->memory.low; > > parent = parent_mem_cgroup(memcg); > + if (!parent) > + return MEMCG_PROT_NONE; > + > if (parent == root_mem_cgroup) > goto exit; > > -- > 2.14.3 >