From patchwork Fri Apr 24 03:50:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 220652 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.9 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97F55C55186 for ; Fri, 24 Apr 2020 03:50:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 77B3320706 for ; Fri, 24 Apr 2020 03:50:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="LMS8YAB/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726383AbgDXDuu (ORCPT ); Thu, 23 Apr 2020 23:50:50 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:51670 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725823AbgDXDut (ORCPT ); Thu, 23 Apr 2020 23:50:49 -0400 Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 03O3jA4g001586 for ; Thu, 23 Apr 2020 20:50:49 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=facebook; bh=H7J4YB8zRiIn+6Qwl8+W/lRxPsR21nnhgkmf7I6X4V0=; b=LMS8YAB/98OzqC75Dg0IwJW6Oo1L8W+6VeQyBkCGmU3xgc2UOlIhsSC2n+UfcftivCe9 EeQQ9AmPc5HoyhwVz2hCZWszGpFT0WElb8l9gCugJ4146lkIXSemvJ7IA69Qm4/bzkPi OpWdiLUuHPoOaJO4AfhQP6TJeoxq0kuvCNs= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 30kkpe1p76-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 23 Apr 2020 20:50:49 -0700 Received: from intmgw005.03.ash8.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1847.3; Thu, 23 Apr 2020 20:50:48 -0700 Received: by devbig012.ftw2.facebook.com (Postfix, from userid 137359) id 2520D2EC2B09; Thu, 23 Apr 2020 20:50:45 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Andrii Nakryiko Smtp-Origin-Hostname: devbig012.ftw2.facebook.com To: , , , CC: , , Andrii Nakryiko Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH bpf] bpf: fix leak in LINK_UPDATE and enforce empty old_prog_fd Date: Thu, 23 Apr 2020 20:50:39 -0700 Message-ID: <20200424035039.3534080-1-andriin@fb.com> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138, 18.0.676 definitions=2020-04-23_19:2020-04-23,2020-04-23 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 spamscore=0 priorityscore=1501 suspectscore=8 mlxscore=0 lowpriorityscore=0 phishscore=0 adultscore=0 malwarescore=0 clxscore=1015 mlxlogscore=895 impostorscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004240025 X-FB-Internal: deliver Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Fix bug of not putting bpf_link in LINK_UPDATE command. Also enforce zeroed old_prog_fd if no BPF_F_REPLACE flag is specified. Signed-off-by: Andrii Nakryiko --- kernel/bpf/syscall.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index d85f37239540..087cf27218c9 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3608,7 +3608,7 @@ static int link_create(union bpf_attr *attr) static int link_update(union bpf_attr *attr) { - struct bpf_prog *old_prog = NULL, *new_prog; + struct bpf_prog *old_prog = NULL, *new_prog = NULL; struct bpf_link *link; u32 flags; int ret; @@ -3628,31 +3628,38 @@ static int link_update(union bpf_attr *attr) return PTR_ERR(link); new_prog = bpf_prog_get(attr->link_update.new_prog_fd); - if (IS_ERR(new_prog)) - return PTR_ERR(new_prog); + if (IS_ERR(new_prog)) { + ret = PTR_ERR(new_prog); + new_prog = NULL; + goto out_put; + } if (flags & BPF_F_REPLACE) { old_prog = bpf_prog_get(attr->link_update.old_prog_fd); if (IS_ERR(old_prog)) { ret = PTR_ERR(old_prog); old_prog = NULL; - goto out_put_progs; + goto out_put; } + } else if (attr->link_update.old_prog_fd) { + ret = -EINVAL; + goto out_put; } #ifdef CONFIG_CGROUP_BPF if (link->ops == &bpf_cgroup_link_lops) { ret = cgroup_bpf_replace(link, old_prog, new_prog); - goto out_put_progs; + goto out_put; } #endif ret = -EINVAL; -out_put_progs: +out_put: if (old_prog) bpf_prog_put(old_prog); - if (ret) + if (ret && new_prog) bpf_prog_put(new_prog); + bpf_link_put(link); return ret; }