Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp4792119ybf; Wed, 4 Mar 2020 10:45:08 -0800 (PST) X-Google-Smtp-Source: ADFU+vvLQCV+KjgdiHMleusMxJxWVwfBBVA7UdR4nVj9gCzTEETHQWWMzAByoIDW54V/GjPhS7/6 X-Received: by 2002:a05:6808:b1c:: with SMTP id s28mr2759903oij.2.1583347508501; Wed, 04 Mar 2020 10:45:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583347508; cv=none; d=google.com; s=arc-20160816; b=YuXyWeweTL+pl4N4twsE/Bp8aaUY41pNYFWslZc7LTWJKD6qtiUdi2L/+olwv21yXw e1znVyt4AahYgszlRtfL75iVxkBVV9/tLRrECjivUfej5dOSVi2XKcZGlP0z/+Wr8qhk kW0NgHvzMnfGmEeawCe4DDmaFSIGUQaWQtRPM2JH3A8iffb7fcP/36QTy/q4nrai0Kp/ sq77+SFwyMebKmU1WSIDNM2/e4cWmdDkUHzxsutnfnBuAstn2pAhtf2jA8ukJiFqJmAB YxX9YGdYGobIHRTkCyWEwQVoTo3DOILWIlLxx76H0yyplz/QWpOxUqFPUHvQxAzd2fHh bYfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:date:from:dkim-signature; bh=McYCjFd7DNHcc98fHYBjU5qExu0x6OSshDp3fc3A0zc=; b=XhOEUeuPxGZ0lc2l/lfuKsYWLJ/Y/pknjjXi1UAg6cta/hGV0Ns+WQqQaaFWz36I8J vfKNOXDSADpsQ0K9TU07hu85OxxzRShnT83I+msn0+Yezro9LfRMQjFHXWPXk/SyOKjl uBWKu16vq9xw7Vls0VaRQ+r81DxXRFVSzV8PgjF+BNC/Wgz/q8OGbeyekwUs5nJX/szp 7izNSRolDMah4rFEtIujZ6CFp+F3hzZukehwXWw6/n1pH1DkrmJCEtOK7eIEGDc+QNqz 9daHE2AuYRI1EE+GkERjw1BayvLAQMYie6wf7yvEnCv+DsYhnpSWZz0VfV3DAi77Rw2m LMbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FSyk0RND; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q81si1733392oic.174.2020.03.04.10.44.56; Wed, 04 Mar 2020 10:45:08 -0800 (PST) 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=@chromium.org header.s=google header.b=FSyk0RND; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730132AbgCDSop (ORCPT + 99 others); Wed, 4 Mar 2020 13:44:45 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:56032 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729965AbgCDSoo (ORCPT ); Wed, 4 Mar 2020 13:44:44 -0500 Received: by mail-wm1-f65.google.com with SMTP id 6so3361594wmi.5 for ; Wed, 04 Mar 2020 10:44:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:date:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=McYCjFd7DNHcc98fHYBjU5qExu0x6OSshDp3fc3A0zc=; b=FSyk0RNDcJgmtsUCb5U3SP0sB3JM7GBEhNfl/HKWYonQ92L1E5AgCodzJTjDIS0lZQ iDTBH/Ozz1R+wL1OEZaX7dDMKzwjND4A7r4AJsxeYy5UxbF8rDU0yri2C0fraZbVLFGp 2EPwzUp9CcVrzBsGO7IE+y/kEbmZcKMUJznNo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=McYCjFd7DNHcc98fHYBjU5qExu0x6OSshDp3fc3A0zc=; b=EmNdSBEh+h5fuuMl9MtpQgE8eukZT540wM3lhfDsroDe9fnj+66QYNm9hidnYhf8iG u21eZPmeE40xf93vRX1UMOJjU0XjOXF0PfTaFYcmfRUpWA3sJJdx7axx1/zZ2Le6XxXP +2nZU5oS3QjNsa/j6EU++1YewZsv02h2/B3Yo7cYu5DaNjCsL8x0tVSan/2H4VRK2D1g 9HpheN5sZ3HYcHwJVedlThWtRSHqfDqkW2+VzB5VyH2paruoZ/XL7zAz1dd4MdPXT2M4 5bJENn3ih6bAGOzb/+KQpgp1U8QBn0nFZEZRXET64dlITglyJYlWxcIgZt/uiaEjH8gy QBwA== X-Gm-Message-State: ANhLgQ3pSLZYIb4WO4kGtFgq62g7l0M7IeMZSNgIcExmqbv1bsHZiKu9 az049YwZmSYcSb9jGDwbePnoXg== X-Received: by 2002:a05:600c:249:: with SMTP id 9mr4786974wmj.186.1583347483522; Wed, 04 Mar 2020 10:44:43 -0800 (PST) Received: from chromium.org (77-56-209-237.dclient.hispeed.ch. [77.56.209.237]) by smtp.gmail.com with ESMTPSA id d63sm5340119wmd.44.2020.03.04.10.44.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 10:44:42 -0800 (PST) From: KP Singh X-Google-Original-From: KP Singh Date: Wed, 4 Mar 2020 19:44:41 +0100 To: Daniel Borkmann Cc: linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Andrii Nakryiko , Alexei Starovoitov , Paul Turner , Jann Horn , Florent Revest , Brendan Jackman Subject: Re: [PATCH bpf-next v3 1/7] bpf: Refactor trampoline update code Message-ID: <20200304184441.GA25392@chromium.org> References: <20200304154747.23506-1-kpsingh@chromium.org> <20200304154747.23506-2-kpsingh@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 04-M?r 19:37, Daniel Borkmann wrote: > On 3/4/20 4:47 PM, KP Singh wrote: > > From: KP Singh > > > > As we need to introduce a third type of attachment for trampolines, the > > flattened signature of arch_prepare_bpf_trampoline gets even more > > complicated. > > > > Refactor the prog and count argument to arch_prepare_bpf_trampoline to > > use bpf_tramp_progs to simplify the addition and accounting for new > > attachment types. > > > > Signed-off-by: KP Singh > > Acked-by: Andrii Nakryiko > > [...] > > diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c > > index c498f0fffb40..9f7e0328a644 100644 > > --- a/kernel/bpf/bpf_struct_ops.c > > +++ b/kernel/bpf/bpf_struct_ops.c > > @@ -320,6 +320,7 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, > > struct bpf_struct_ops_value *uvalue, *kvalue; > > const struct btf_member *member; > > const struct btf_type *t = st_ops->type; > > + struct bpf_tramp_progs *tprogs = NULL; > > void *udata, *kdata; > > int prog_fd, err = 0; > > void *image; > > @@ -425,10 +426,18 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, > > goto reset_unlock; > > } > > + tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL); > > + if (!tprogs) { > > + err = -ENOMEM; > > + goto reset_unlock; > > + } > > + > > Looking over the code again, I'm quite certain that here's a memleak > since the kcalloc() is done in the for_each_member() loop in the ops > update but then going out of scope and in the exit path we only kfree > the last tprogs. You're right, nice catch. Fixing it. - KP > > > + tprogs[BPF_TRAMP_FENTRY].progs[0] = prog; > > + tprogs[BPF_TRAMP_FENTRY].nr_progs = 1; > > err = arch_prepare_bpf_trampoline(image, > > st_map->image + PAGE_SIZE, > > &st_ops->func_models[i], 0, > > - &prog, 1, NULL, 0, NULL); > > + tprogs, NULL); > > if (err < 0) > > goto reset_unlock; > > @@ -469,6 +478,7 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, > > memset(uvalue, 0, map->value_size); > > memset(kvalue, 0, map->value_size); > > unlock: > > + kfree(tprogs); > > mutex_unlock(&st_map->lock); > > return err; > > }