Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp1318146imc; Mon, 11 Mar 2019 11:02:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqwQ/z2EvshwdBssD5p1KdeZTnb0qlE67F1QAt505Fgunecz4kud3ntU+KOLdlRNwouT7K+t X-Received: by 2002:a17:902:1101:: with SMTP id d1mr34116089pla.19.1552327328045; Mon, 11 Mar 2019 11:02:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552327328; cv=none; d=google.com; s=arc-20160816; b=Mu+pnuWj6ynZ9FcOK+ToT/EL+5zMmfIop3doekOf+XsM71RRcFBU9oMurxMCCAKGMU MQXbqpqMBu8QrxzE1dQSpjAWuVPREe8RiK5rYxbEQn70FmxDS8NpLkA+dYs3QNifoGGZ I1M9fsyL8KGMcyPmeMh9wQsdj6gMgmeMCigzI51FnaVZxqKkQ5tAmrL48TTSLdWmzHb5 5kLctPao6IjtBTosMWsJE8a7o+G9tBQJCD5ry5jZzOOAkoQAfrOur10GVy3fa1wOxMaP R56RMH3wyHOQ1wfGrGnOcAQd8+yll5KzA+lFtdmxO+AwkGf2+BVo6b0QwENT/800ziBZ qaSQ== 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-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=+SdCLIuhpcmrikRZBEqNLVD299M+XmjQ/x6GgQUzO38=; b=M/mF8nMisT7WlNdekcDfWEMdWwHHxZf69E/FFWfUXn8JzgkfuWhG07HcUVVwVRyYIP qE+w8Mna8FVPyecTxjkL13UcfmjPSjFsTgtWrccERbAEsau8t7mBc9YDSZXP5EYwRWK/ UpPN/BJ3urXPFkjyYdF4qbJa7pFZNNrmwvZSH4sYkXR0fWmiak5nRNcrsH+7DLXFILhd 60e0ETAezeRG7oueAbX57yVny7DFSYAxNVndOLbFfo9qVTpU0SDu4YKSYPv+mLgPuXLt A93MF4EMB93GINcnkd6tGMg1aplBTSvSbf/NZ0bo6+VaU/FmcVgUYowQL12CgBxYcdGJ mluA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=L3MtkoTd; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x20si5284355pfm.282.2019.03.11.11.01.51; Mon, 11 Mar 2019 11:02:08 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=L3MtkoTd; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727527AbfCKSBN (ORCPT + 99 others); Mon, 11 Mar 2019 14:01:13 -0400 Received: from mail-lf1-f65.google.com ([209.85.167.65]:36800 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726864AbfCKSBN (ORCPT ); Mon, 11 Mar 2019 14:01:13 -0400 Received: by mail-lf1-f65.google.com with SMTP id 197so4140496lfz.3; Mon, 11 Mar 2019 11:01:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=+SdCLIuhpcmrikRZBEqNLVD299M+XmjQ/x6GgQUzO38=; b=L3MtkoTdHBCRN6CfMIpbFERqEEAep41cZ1Kz7rrUDteFJ6PBr2SVnh6ei5K5c71z9Q OwS/ZFIt/OpQgMeBZvpjzeyNCDbCM2AsdpbVIPFphY3C++3bWqtTOb630BL5EXrLku3X 8ixzQaGwvuYEAWW1ZDZP75uaR4Uz5b8obrsUgc9r0Yff3X4nXP7bOFBDSVt/M+oAyeBL ul2FWIDs2CVqD84G7U4QyNoOQvaUwSIsDxafnH2PZBRvuw87MzIO3D5/z8QzZO7qC18L KbGKYW+GhyAMgQPcM8T9PAaroq02jCywdPE3TrC3cZMpTvP4ZN7j0KicLJVkwYXiy3tE A8ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=+SdCLIuhpcmrikRZBEqNLVD299M+XmjQ/x6GgQUzO38=; b=rAXaOlftmQnBHgDd83NVC6Bw07djlN+017OyV+cFHWXwWgbQw9JCj2aO9b6sdk9uXw UDBdZYllI051g/S2bVhgHmmkiBZrC0KcsSMsuzPHpiblp2MhVqVyySBTLgPQsNVVzRs9 FfUkjA9Vnki8STJYxWpJUaudu8TFzqUMkGupvKHjFumZk4f+jLFUZTQnEZeOuhbKI+GW aO2TNlcs3jASBPHHhV6Rcr+NmZVbkCSAl58XPZ1yJ105lZ0EkYincTuA8M5ba07FxNWG aw5NwbwKKM4Q55DivWnqUYzzE21opiFFkiWxAUDqNLbKd8okiA5VtzRhGnF4taleHagJ WRZg== X-Gm-Message-State: APjAAAWoYKRuKKma9xHTQS1kivaIUicGhRS72IhEzivN7/LmBTSF/6ok A8CZYCkMVwIJZXBTkAtWWpKVK0IOT+w= X-Received: by 2002:a19:d602:: with SMTP id n2mr17896764lfg.165.1552327270932; Mon, 11 Mar 2019 11:01:10 -0700 (PDT) Received: from localhost (89-64-59-228.dynamic.chello.pl. [89.64.59.228]) by smtp.gmail.com with ESMTPSA id m16sm1040421ljb.50.2019.03.11.11.01.09 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 11 Mar 2019 11:01:10 -0700 (PDT) Date: Mon, 11 Mar 2019 19:00:44 +0100 From: Tomasz Duszynski To: Sven Van Asbroeck Cc: Peter Rosin , Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] iio: envelope-detector: fix use-after-free on device remove Message-ID: <20190311180042.GA17880@arch> References: <20190310193246.31761-1-TheSven73@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20190310193246.31761-1-TheSven73@gmail.com> User-Agent: Mutt/1.11.3 (2019-02-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Mar 10, 2019 at 03:32:46PM -0400, Sven Van Asbroeck wrote: > This driver's remove path never explicitly cancels the > delayed work. So it is possible for the delayed work to > run after the core has freed the private structure > (struct envelope). This is a potential use-after-free. > > Fix by adding a devm_add_action callback to the remove > path, called right after iio_device_unregister(), which > explicitly cancels the delayed work. > > This issue was detected with the help of Coccinelle. > > Signed-off-by: Sven Van Asbroeck > --- > drivers/iio/adc/envelope-detector.c | 12 ++++++++++++ > 1 file changed, 12 insertions(+) > > diff --git a/drivers/iio/adc/envelope-detector.c b/drivers/iio/adc/envelope-detector.c > index 2f2b563c1162..2f1c78b3ff44 100644 > --- a/drivers/iio/adc/envelope-detector.c > +++ b/drivers/iio/adc/envelope-detector.c > @@ -321,6 +321,14 @@ static const struct iio_info envelope_detector_info = { > .read_raw = &envelope_detector_read_raw, > }; > > +static void envelope_detector_stop_work(void *data) > +{ > + struct iio_dev *indio_dev = data; > + struct envelope *env = iio_priv(indio_dev); > + > + cancel_delayed_work_sync(&env->comp_timeout); > +} > + > static int envelope_detector_probe(struct platform_device *pdev) > { > struct device *dev = &pdev->dev; > @@ -395,6 +403,10 @@ static int envelope_detector_probe(struct platform_device *pdev) > return ret; > } > > + ret = devm_add_action(dev, envelope_detector_stop_work, indio_dev); > + if (ret) > + return ret; Just a random thought. Wouldn't devm_add_action_or_reset() be a better fit? In case adding action results in failure we will not get the chance to cancel work. > + > return devm_iio_device_register(dev, indio_dev); > } > > -- > 2.17.1 >