Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp3062934lqz; Wed, 3 Apr 2024 18:01:33 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWHLSB2fa01En9gBN2hy63QxoX27jLGA7sHU2mchtzeyJyM/kS+ce82DBim8d13OqbixhplV3aC6ltFzJc2aruEUFeDqcUrKmpIklHMNQ== X-Google-Smtp-Source: AGHT+IFpc9K2bXxxlzg9MwkE0gU/BDDXMdZtXSQ2f6dzbZMuR8POnorXnRb6rSvRI0BxiUy5d8dV X-Received: by 2002:a05:6602:341e:b0:7d3:51e9:5ab5 with SMTP id n30-20020a056602341e00b007d351e95ab5mr560964ioz.11.1712192493222; Wed, 03 Apr 2024 18:01:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712192493; cv=pass; d=google.com; s=arc-20160816; b=zu3ZyT45V1pmc01ots2aErtMQK0wI7SU+B9qyksQ1CE2MyEx9KA/Zh44LXq9SvK3+2 HkRXbVcFgT6USW6SZome6kVVmL/RQ4ARh5h78rgTHNDhuCK1Yu/+UfvRWrdm4Z5wS7kG j/JuLvOIKphdP6wNzHP+puMGMwPu32JPG+n3lQag4TAvXogK274fNbaF/P9+mUv03iNw NVedXaxk9n6/ZJDhMHlPlfq5VLoGjWAMgakMgar8MohdGowXAvqzu0wY9IkDRKg08WJR w1hdW1xH439H2DSUH5Q30gtkvcFQf7tqHXJlfd0W5NikXRybAbb0XDLP18N977w++9t7 IIzg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=HLU2APTaqoheKuuzuNc2tSXn6E7XDkIQ0oPwW38nA4I=; fh=QyWghf9mI4gQCllpK6DogVUVp1R+ct/qHuz6OYtqE58=; b=PsqP10n/Vea0U8AlyCSSD9RzhUffNIZBD4hl1dyTeruz59kQtGskjFrUpfAqHa8vSX 1jIjUK0zdxS1p15gb23wFByrD2YPML6g2qGbm+KOAUlC89yCvIAvM1KsPqTb0tqWHn9I XCvxaVdI1+Dwr5HoVEdTQAp/OGRzIjGYCCHXy2dxU5KikSYDF6algKFV2vFyZU/UuECl pZpAsajIUedbWtFM5U46MsExRHhsDqzF0pEp7+M/UCvJq8y4jRCjuDi1e36uCw9gakX7 Obn3Je8+zshecbrKpnuksgPDXmRYpv6IoHRNhVumNjTgH1zAjJyVsF7X3QU+1pIDw/fY 2Seg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Xt5Dbdpn; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-130798-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130798-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id y27-20020a637d1b000000b005e43a9f911fsi14214649pgc.53.2024.04.03.18.01.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 18:01:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-130798-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Xt5Dbdpn; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-130798-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130798-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id DA8A628194F for ; Thu, 4 Apr 2024 01:01:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 751DF8F48; Thu, 4 Apr 2024 01:01:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Xt5Dbdpn" Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E46D123CE; Thu, 4 Apr 2024 01:01:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712192482; cv=none; b=jDVuDnglQkvluN2ShVF/olh4YI5RVtuY1l46MYY63suJMzR8R4u9wOjro3cO5/bF+q7Vo4dNbrXxv+1S6xGpqZ53RhuAI0LPvrr4V7g+v881SER83i8g2e8Xfw/B99iJUFwMkNoIknm3/d4SIcYuJfT8WW5V5iWVXBtLUvA65jk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712192482; c=relaxed/simple; bh=VcITItGHjtKY9dfEMl4G05gYTHqgBMk3fUlh/7jjiJc=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=lMJC7dYTlJX1NWVGmvVzpvPyVBb+0Mq+SUmpaT6L/h8gwR8Ac/hykqLDJ3Dse61PqKc4ktisdjksuTtli8kC6l7hRp9v6A2mYoMcKWI3Hfw1ImgTZQ9aOP+mgkbLwyEiUXcnNTbxODDYRFCFTyq6c9dEmBF9Tztzgt8KQAf2hHw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Xt5Dbdpn; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-33ddd1624beso207012f8f.1; Wed, 03 Apr 2024 18:01:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712192479; x=1712797279; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=HLU2APTaqoheKuuzuNc2tSXn6E7XDkIQ0oPwW38nA4I=; b=Xt5DbdpnBGpOIWyRpSnbHEtDXN0wnQd1F7Qou3PIzQSoe/wf57mdql9R4FJxiiEG8V ffsNkTEMPFwGCSxykrCWYEGIs6Wfe7a7zsY3RdSe0zSxjT6u8TdQIiSh/4oCPfCOhTDO j0WwvGfhZ1EWzyzN1R9AoXt0sEFgSfJE83YffBaiIWuMPh5Y+B6Z11Bo9jsQ81av5LyF QtLzYxRkuZJEG3qndqKCs/GqMLfpN0JKA0yG6T/AenSuPMGyfr8JO+3XZU04C4mK4U9Z ObN5UKhUnGc0fe7Xay/5W1qRSr+wvhmXqtMQqej4Y485rY/kkB8cH6bYU++b+CQC3SWd QX+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712192479; x=1712797279; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HLU2APTaqoheKuuzuNc2tSXn6E7XDkIQ0oPwW38nA4I=; b=bzfBPktrhhOsbsS+4XdoHH2WMXxsxZcSSS2CucFW0NKFL4z5ciDQVUCnv4iIPr4Z4w wbpjHip8I7GYxGfakJoVFBF2Tl4olddnDnrfatE/1zObmB/EPGVPmr5basbxFtqNt2kv OJuZSJb1boqWJU2GX10sbAcMQnDA58dnK7y7G4CKVp5vRQGOyVRcxRt2ehXd/IwTW7VC 1Fy7LwsselMEiSyGRMQAwmMiEWKxIRZUJoft8BLpUqzEtxhFADfpHqMem8sbHVwxtWG2 g6UrbU2tW3JaNdsZ0rP7XJr89faXo3LAAsHXDD+DKVWzOoGZETkzcXN4M/GsdwwGIzNe Q9CQ== X-Forwarded-Encrypted: i=1; AJvYcCU3ovBuL1GzsPXy1KvQfVa2F2DnNXRdTyA1ELG2hSRDyB696Lkln7EICeEEZnfVT4bM/sawSnf8UdJefkKzsmsOmj/c1WRJzUBU1NOPCAx+nlx2Qk5/JwRijQVi5MK7jfIG6q4S32G415oHOSUsrjYLLLojLvAa187oi5J9alTmUGql X-Gm-Message-State: AOJu0YwpKjn+lYaIO+pa8wrRYQWwsjyXrCr5rWtbykkQnExEPA87Uqun ukuWJPTt9S5gD1Txn4qWXXgPNIzAxXDUy/xIlXN72qIYASyXnALnvlzklyVbZzW+nNmCVKTCk3F VLB8rJ89gbH5//dE6v29bLmAr3gQ= X-Received: by 2002:a05:6000:4f1:b0:343:6c07:c816 with SMTP id cr17-20020a05600004f100b003436c07c816mr1064770wrb.16.1712192478995; Wed, 03 Apr 2024 18:01:18 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240322-hid-bpf-sleepable-v5-0-179c7b59eaaa@kernel.org> <20240322-hid-bpf-sleepable-v5-1-179c7b59eaaa@kernel.org> In-Reply-To: From: Alexei Starovoitov Date: Wed, 3 Apr 2024 18:01:07 -0700 Message-ID: Subject: Re: [PATCH bpf-next v5 1/6] bpf/helpers: introduce sleepable bpf_timers To: Benjamin Tissoires Cc: Benjamin Tissoires , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , bpf , LKML , "open list:KERNEL SELFTEST FRAMEWORK" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Apr 3, 2024 at 11:50=E2=80=AFAM Alexei Starovoitov wrote: > > On Wed, Mar 27, 2024 at 10:02=E2=80=AFAM Benjamin Tissoires > wrote: > > > > goto out; > > > > } > > > > + spin_lock(&t->sleepable_lock); > > > > drop_prog_refcnt(t); > > > > + spin_unlock(&t->sleepable_lock); > > > > > > this also looks odd. > > > > I basically need to protect "t->prog =3D NULL;" from happening while > > bpf_timer_work_cb is setting up the bpf program to be run. > > Ok. I think I understand the race you're trying to fix. > The bpf_timer_cancel_and_free() is doing > cancel_work() > and proceeds with > kfree_rcu(t, rcu); > > That's the only race and these extra locks don't help. > > The t->prog =3D NULL is nothing to worry about. > The bpf_timer_work_cb() might still see callback_fn =3D=3D NULL > "when it's being setup" and it's ok. > These locks don't help that. > > I suggest to drop sleepable_lock everywhere. > READ_ONCE of callback_fn in bpf_timer_work_cb() is enough. > Add rcu_read_lock_trace() before calling bpf prog. > > The race to fix is above 'cancel_work + kfree_rcu' > since kfree_rcu might free 'struct bpf_hrtimer *t' > while the work is pending and work_queue internal > logic might UAF struct work_struct work. > By the time it may luckily enter bpf_timer_work_cb() it's too late. > The argument 'struct work_struct *work' might already be freed. > > To fix this problem, how about the following: > don't call kfree_rcu and instead queue the work to free it. > After cancel_work(&t->work); the work_struct can be reused. > So set it up to call "freeing callback" and do > schedule_work(&t->work); > > There is a big assumption here that new work won't be > executed before cancelled work completes. > Need to check with wq experts. > > Another approach is to do something smart with > cancel_work() return code. > If it returns true set a flag inside bpf_hrtimer and > make bpf_timer_work_cb() free(t) after bpf prog finishes. Looking through wq code... I think I have to correct myself. cancel_work and immediate free is probably fine from wq pov. It has this comment: worker->current_func(work); /* * While we must be careful to not use "work" after this, the trace * point will only record its address. */ trace_workqueue_execute_end(work, worker->current_func); the bpf_timer_work_cb() might still be running bpf prog. So it shouldn't touch 'struct bpf_hrtimer *t' after bpf prog returns, since kfree_rcu(t, rcu); could have freed it by then. There is also this code in net/rxrpc/rxperf.c cancel_work(&call->work); kfree(call); So it looks like it's fine to drop sleepable_lock, add rcu_read_lock_trace() and things should be ok.