From patchwork Tue Jan 24 12:48:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Windsor X-Patchwork-Id: 9535145 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 0A82B60434 for ; Tue, 24 Jan 2017 12:48:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F3EE227F17 for ; Tue, 24 Jan 2017 12:48:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E74C427E3E; Tue, 24 Jan 2017 12:48:45 +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.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 21BB627E3E for ; Tue, 24 Jan 2017 12:48:44 +0000 (UTC) Received: (qmail 27992 invoked by uid 550); 24 Jan 2017 12:48:41 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 27954 invoked from network); 24 Jan 2017 12:48:39 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=yvyoaOY1WQCkuWbuG/Jy3fYajVWvZGE+1yPdFeHI324=; b=bE1sM7ZfSOvPYT7T4UEzxY6sndfP4xuUEyGHPt//pEdzMAOWzBwz3ssLTcoBMX7jIO YhvFY1jT8izeSip34kcULeBpygg/kRpqGJpWMYz8OuRwEQ0MeJDPBVlaqoGmpADOGqIx 4qODOfHUIFXutrw1sSr5pTJlXxzL1R838wCqcPKP/6mK5FMwaM0f9ZvZZSQRmnP64Fym D9OoYMDO3HYrOr0ngIg7nblkoV0NCF/LdO2KR+M3q3T5PIwAUnxMwIgvo4apQj3dYMOs i/j3RqbEUOrXp2F0mN45RIMoUoJYIKBJ5qC36rN/QigHTpDOGGQyj8gfClIH5Gj8mW7c NwDQ== 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; bh=yvyoaOY1WQCkuWbuG/Jy3fYajVWvZGE+1yPdFeHI324=; b=fxZ7S+hQLtYi4jj9dn+17+6Y+MO8kH6CrmkKRkB+cW+QNbOItyNgkW0c8gZRiUsJY5 sZwzCq4Cem5TANM78aSkq+H36QseUsoGvmvK9Ld6ZrUejrSJOkg5qC0DbYdYlEfiTSAa fVURktMUsr9tHEcrzZq4S2szIEfZSRh4EVFc1YsBmh/tLjf/3zidXXEdp0EUYZV8fYtk VU5ofjNHB1pwUmGx9uNlwxsrYaJgF/nrWONtLuFwV1QjvfHQxnl00ird57RD5MKdSSCH ssxXZm2LeLGYxL+FLZRwVXFOFnCFn6JyHpj40HZ+qbjIrTC56/03a/1p9/kCEc9akcb6 Pr5w== X-Gm-Message-State: AIkVDXJxIIyg0OeCLvdQs7btsOw9sTKzDYh0EPGZ+gQlXBtXe1vc5EHVhOPXnUcXuGXbfA== X-Received: by 10.55.7.14 with SMTP id 14mr28375691qkh.224.1485262108314; Tue, 24 Jan 2017 04:48:28 -0800 (PST) From: David Windsor To: netdev@vger.kernel.org, kernel-hardening@lists.openwall.com Cc: keescook@chromium.org, elena.reshetova@intel.com, ja@ssi.bg, dwindsor@gmail.com, ishkamiel@gmail.com Date: Tue, 24 Jan 2017 07:48:20 -0500 Message-Id: <1485262100-12366-1-git-send-email-dwindsor@gmail.com> X-Mailer: git-send-email 2.7.4 Subject: [kernel-hardening] [PATCH net] net: free ip_vs_dest structs when refcnt=0 X-Virus-Scanned: ClamAV using ClamSMTP Currently, the ip_vs_dest cache frees ip_vs_dest objects when their reference count becomes < 0. Aside from not being semantically sound, this is problematic for the new type refcount_t, which will be introduced shortly in a separate patch. refcount_t is the new kernel type for holding reference counts, and provides overflow protection and a constrained interface relative to atomic_t (the type currently being used for kernel reference counts). Per Juilan Anastasov: "The problem is that dest_trash currently holds deleted dests (unlinked from RCU lists) with refcnt=0." Changing dest_trash to hold dest with refcnt=1 will allow us to free ip_vs_dest structs when their refcnt=0, in ip_vs_dest_put_and_free(). Signed-off-by: David Windsor --- include/net/ip_vs.h | 2 +- net/netfilter/ipvs/ip_vs_ctl.c | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/include/net/ip_vs.h b/include/net/ip_vs.h index cd6018a..a3e78ad 100644 --- a/include/net/ip_vs.h +++ b/include/net/ip_vs.h @@ -1421,7 +1421,7 @@ static inline void ip_vs_dest_put(struct ip_vs_dest *dest) static inline void ip_vs_dest_put_and_free(struct ip_vs_dest *dest) { - if (atomic_dec_return(&dest->refcnt) < 0) + if (atomic_dec_and_test(&dest->refcnt)) kfree(dest); } diff --git a/net/netfilter/ipvs/ip_vs_ctl.c b/net/netfilter/ipvs/ip_vs_ctl.c index 55e0169..6b5492e 100644 --- a/net/netfilter/ipvs/ip_vs_ctl.c +++ b/net/netfilter/ipvs/ip_vs_ctl.c @@ -711,7 +711,6 @@ ip_vs_trash_get_dest(struct ip_vs_service *svc, int dest_af, dest->vport == svc->port))) { /* HIT */ list_del(&dest->t_list); - ip_vs_dest_hold(dest); goto out; } } @@ -1084,7 +1083,6 @@ static void __ip_vs_del_dest(struct netns_ipvs *ipvs, struct ip_vs_dest *dest, list_add(&dest->t_list, &ipvs->dest_trash); dest->idle_start = 0; spin_unlock_bh(&ipvs->dest_trash_lock); - ip_vs_dest_put(dest); } @@ -1160,7 +1158,7 @@ static void ip_vs_dest_trash_expire(unsigned long data) spin_lock(&ipvs->dest_trash_lock); list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) { - if (atomic_read(&dest->refcnt) > 0) + if (atomic_read(&dest->refcnt) > 1) continue; if (dest->idle_start) { if (time_before(now, dest->idle_start +