Received: by 10.192.165.148 with SMTP id m20csp541187imm; Wed, 25 Apr 2018 03:59:35 -0700 (PDT) X-Google-Smtp-Source: AIpwx48YnhXHc8nnE1E7y/jQbs+DYnZ0f/plQWO3CjJGcKskD9AkrwHQ2DqWvqAdJXN53TiXRU7d X-Received: by 10.99.67.65 with SMTP id q62mr23910433pga.358.1524653975207; Wed, 25 Apr 2018 03:59:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524653975; cv=none; d=google.com; s=arc-20160816; b=tCjC5KNgdzdTvhnkb3rSs8UzL7i53iBRJoA5DvQKKn7BgeX/ioAowvlgmHEjM5LamV S+Vl5YmHXIYeq0Pl2apyMzGakmjoMSGtStsj6O8qFfBQd5vpr1IM3Nya6jmOjzEN1NlK QPC8Wfnr28/Z0Qfs82ky43NeJ9rPiwtUp16UN0yIRW0YfA4l16hDJUp7Tn7YfMT8H/XR nZWhE4OxD1KWumKvp8W8scrHQhGiHS9vWx1HenD+9wiq76+ulqQzrT4e8RTRdga3lexP M1iDJBvTbu6q/AbT0fdWb/FSeMKD4HCnAzEy6F2IAqGz0Oxlb08pk3Qq6Ptllbtcq2zY FNBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=UePeVIGylLexUIgcc1hilub4IQ7ujy2nJM3fKRCAHoU=; b=YYX4MyfWnOTu7FZgvZ56zlBTFQW8IF1tHcQmJ3nxQXDnzeG3WTA51XYtpSCD+qmaQB xxq8F0RxlA3wHy8pRM6Ol9amAXMqfDPtQ2LMdoHUKa8tf8ztrU3den3mhED+Z0JpLY20 LiY7TAVLZUfARqGDpI5tSHLR0PBsAQtN0qvgveZaSIvmqqBKBQFR4LwvtluzmSWiqrO7 2NF4Dtu5Z82kld4XwZqo62ETBrqhL0OyQJiSW47t3P01UoXkOZJmEeeI/B4Pcl9QUHhR h23GZDBPRCmJMR3j7ms+pKoxmhoA6octTrpSk+l3cULHLZc+FE71AUirzLiz99OEE8/s D3fA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r14-v6si17001982plj.341.2018.04.25.03.59.20; Wed, 25 Apr 2018 03:59:35 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753662AbeDYK5e (ORCPT + 99 others); Wed, 25 Apr 2018 06:57:34 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:52860 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754174AbeDYKnF (ORCPT ); Wed, 25 Apr 2018 06:43:05 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id ED86736; Wed, 25 Apr 2018 10:43:04 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, John Fastabend , Daniel Borkmann , Sasha Levin Subject: [PATCH 4.14 141/183] bpf: sockmap, fix leaking maps with attached but not detached progs Date: Wed, 25 Apr 2018 12:36:01 +0200 Message-Id: <20180425103248.215506927@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180425103242.532713678@linuxfoundation.org> References: <20180425103242.532713678@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 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 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: John Fastabend [ Upstream commit 3d9e952697de89b53227f06d4241f275eb99cfc4 ] When a program is attached to a map we increment the program refcnt to ensure that the program is not removed while it is potentially being referenced from sockmap side. However, if this same program also references the map (this is a reasonably common pattern in my programs) then the verifier will also increment the maps refcnt from the verifier. This is to ensure the map doesn't get garbage collected while the program has a reference to it. So we are left in a state where the map holds the refcnt on the program stopping it from being removed and releasing the map refcnt. And vice versa the program holds a refcnt on the map stopping it from releasing the refcnt on the prog. All this is fine as long as users detach the program while the map fd is still around. But, if the user omits this detach command we are left with a dangling map we can no longer release. To resolve this when the map fd is released decrement the program references and remove any reference from the map to the program. This fixes the issue with possibly dangling map and creates a user side API constraint. That is, the map fd must be held open for programs to be attached to a map. Fixes: 174a79ff9515 ("bpf: sockmap with sk redirect support") Signed-off-by: John Fastabend Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/sockmap.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) --- a/kernel/bpf/sockmap.c +++ b/kernel/bpf/sockmap.c @@ -601,11 +601,6 @@ static void sock_map_free(struct bpf_map } rcu_read_unlock(); - if (stab->bpf_verdict) - bpf_prog_put(stab->bpf_verdict); - if (stab->bpf_parse) - bpf_prog_put(stab->bpf_parse); - sock_map_remove_complete(stab); } @@ -877,6 +872,19 @@ static int sock_map_update_elem(struct b return err; } +static void sock_map_release(struct bpf_map *map, struct file *map_file) +{ + struct bpf_stab *stab = container_of(map, struct bpf_stab, map); + struct bpf_prog *orig; + + orig = xchg(&stab->bpf_parse, NULL); + if (orig) + bpf_prog_put(orig); + orig = xchg(&stab->bpf_verdict, NULL); + if (orig) + bpf_prog_put(orig); +} + const struct bpf_map_ops sock_map_ops = { .map_alloc = sock_map_alloc, .map_free = sock_map_free, @@ -884,6 +892,7 @@ const struct bpf_map_ops sock_map_ops = .map_get_next_key = sock_map_get_next_key, .map_update_elem = sock_map_update_elem, .map_delete_elem = sock_map_delete_elem, + .map_release = sock_map_release, }; BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, bpf_sock,