From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229567AbhHCQEO (ORCPT ); Tue, 3 Aug 2021 12:04:14 -0400 Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8012C061757 for ; Tue, 3 Aug 2021 09:04:02 -0700 (PDT) Received: by mail-lf1-x131.google.com with SMTP id h2so40614020lfu.4 for ; Tue, 03 Aug 2021 09:04:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=b0sIfA8FpNIM6xTkEutBlL5Ds5MNrhPhPuFTRp6Km9o=; b=ud6qMISVxTlA723piY50eTeuyo+FHZn93PqfpyEEtiatlyYN9RevAajH26iJhGB23V WBcpw8sws05Ll8tUV8qXegCC2rb0GtL7s3UbeL5gpUnM900AtXBjbf0XnU7m5TmMc5hu k5G9kZI8sMHbFyVgFd5Tq5YvWd9DvnqQ3Cg0gIKtDPIbraDjmaKXIQuLfsqgc+v43NIk u8m0Q4fKD/TbsvFgJNKb1HiZJD7jIGlmCHfeVPFhdqAzcYg0c33S8Yk6UdAa2Dg7BIC1 Wa1qQzFtWmRQWwf+fdncOSgJOxr7d9TLnldVbhhUfKAFjsSgrOYW5Upvcg+rcdny8q1m +hYQ== Subject: Re: [PATCH] check_netdev_priv: warn about using netdev priv data after free_netdev References: <20210802210022.5226-1-paskripkin@gmail.com> <20210803150826.GC1931@kadam> From: Pavel Skripkin Message-ID: <41b1499c-3715-30df-b083-159c7d71efcb@gmail.com> Date: Tue, 3 Aug 2021 19:03:59 +0300 MIME-Version: 1.0 In-Reply-To: <20210803150826.GC1931@kadam> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-ID: To: Dan Carpenter Cc: smatch@vger.kernel.org Hi, Dan! On 8/3/21 6:08 PM, Dan Carpenter wrote: > Thanks Pavel! > > It looks really nice. I've applied it. I'll test it tonight and push > tomorrow. > > I don't see any major issues with the check at all, but I have a few > comments below. > > On Tue, Aug 03, 2021 at 12:00:22AM +0300, Pavel Skripkin wrote: >> +static void match_free_netdev(const char *fn, struct expression *expr, void *_arg_no) >> +{ >> + struct expression *arg; >> + const char *name; >> + >> + arg = get_argument_from_call_expr(expr->args, PTR_INT(_arg_no)); >> + if (!arg) >> + return; >> + >> + name = expr_to_var(arg); >> + if (!name) >> + return; >> + >> + set_state(my_id, name, NULL, &freed); >> +} > > There is a new param_key API which would make this function shorter. > > static void free_netdev(struct expression *expr, const char *name, struct symbol *sym, void *data) > { > set_state(my_id, name, NULL, &freed); > } > > Then in the register function you'd add a hooks like this: > > add_function_param_key_hook("free_netdev", &free_netdev, 0, "$", NULL); > add_function_param_key_hook("free_candev", &free_netdev, 0, "$", NULL); > I guess, I missed that API, sorry :( Next time I will use it instead. > Of course, you've already written your own code which works so it's > fine. But that param_key API is really the most awesome thing. > >> + >> +static void match_symbol(struct expression *expr) >> +{ >> + const char *parent_netdev, *name; >> + struct smatch_state *state; >> + > > This hook is run for every variable so it's tempting to add a shortcut > here: > > if (!has_states(my_id)) > return; > >> + name = expr_to_var(expr); >> + if (!name) >> + return; >> + >> + parent_netdev = get_parent_netdev_name(expr); >> + if (!parent_netdev) >> + return; >> + >> + state = get_state(my_id, parent_netdev, NULL); >> + if (state == &freed) >> + sm_error("Using %s after free_{netdev,candev}(%s);\n", name, parent_netdev); >> +} >> + >> +void check_uaf_netdev_priv(int id) >> +{ >> + if (option_project != PROJ_KERNEL) >> + return; >> + >> + my_id = id; >> + >> + add_function_hook("free_netdev", &match_free_netdev, NULL); > > NULL works but INT_PTR(0) is nicer. ;) > >> + add_function_hook("free_candev", &match_free_netdev, NULL); >> + add_modification_hook(my_id, &ok_to_use); >> + add_hook(&match_symbol, SYM_HOOK); >> +} > > Anyway, I think I will probably add the has_states() check but the > rest isn't important. Thank you for applying and your guidelines, I appreciate it :) With regards, Pavel Skripkin