From patchwork Thu Dec 14 17:10:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?G=C3=A9rald_Colangelo?= X-Patchwork-Id: 1876316 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=weblib.eu header.i=@weblib.eu header.a=rsa-sha256 header.s=google header.b=APO5q1JX; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=139.178.88.99; helo=sv.mirrors.kernel.org; envelope-from=netfilter-devel+bounces-365-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [139.178.88.99]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Srf4q1qx7z23nF for ; Fri, 15 Dec 2023 04:10:51 +1100 (AEDT) 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 C2FCD282729 for ; Thu, 14 Dec 2023 17:10:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 45D804CE1C; Thu, 14 Dec 2023 17:10:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=weblib.eu header.i=@weblib.eu header.b="APO5q1JX" X-Original-To: netfilter-devel@vger.kernel.org Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0117FA0 for ; Thu, 14 Dec 2023 09:10:29 -0800 (PST) Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-50bffb64178so9857392e87.2 for ; Thu, 14 Dec 2023 09:10:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=weblib.eu; s=google; t=1702573828; x=1703178628; darn=vger.kernel.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=ypONQvRdAJNNWttIzBjLbVCQ2NR26CJaSbwReWTGte8=; b=APO5q1JXSO/oC8k6AY1L6WGrf5w2yUFn+IngJzr0baQzhKL570EQJlCyFSxqfMC8jo 6zCjVTblc1KPuVGDDZl10MeSpDfWoUhfuH17pDJdhZ9sx40soQGUOUfKuDnxqD9ZpuLm 6idJkf1XoSJN1J05QJpavtse3z9CXv0d204bnIa2JmjH4wawcG/s+A5WE2/Y747DivM0 79j4svSFhcmUusrEZNfSjwt5iJW98oIvnuMxBM7xfvEWn1RnODI6ziaNfLivsA4cSndC +dC4W9h83/yyvwVn30vjpBS76XJz4SUh4GuW+p+XS0JwfVfKHf2Ph671UCC46l2Su6hQ x7cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702573828; x=1703178628; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ypONQvRdAJNNWttIzBjLbVCQ2NR26CJaSbwReWTGte8=; b=HPlYhACofFIr4sqjIGlxlUZuGIfQFWAa7+eR+Tel2Ko5vZLIaUlepbqXxZmp5eeBSH wwWKrFQoQFnzt/zywVqP2WhF9MqLQzPi/YzgTMLT7l67DTONMtiWL9Z3e4V5ogrkNrSd 5cvWDotXXRaJtxU6a/ZkeqRbMiCf0zoCygvU0fsrIjf+FiGqDvOFeIOKOBCq0wX/lL1I UXLvhXIlbODDKLZFUQV7j+scsxnS5wmS2q3DE8qwSHc0s1L2uWCv89/eGdDpr734amMd wC26sgeab4soseVNcy6qt+7v0BWEckzwp+CJXgOZKfH9/vVdGa6tsQ9aQCBP+YufUOTQ iiWg== X-Gm-Message-State: AOJu0Yxd+MoYJAOjQA7i+Xyoe7Sj8yNqNDRrQey/awlK9vFSWMEBditb DFPU4oJAAbr+R/CJnOmZgg/tgDXt2jfcGZuPHYG7RZgehxPQFwa4 X-Google-Smtp-Source: AGHT+IF/u+VWHv9HmJxXtceM/dnvGmru1IKG1W1S2ZetBtdglDwSJ9pmJfMW0nZj3zhzbHGU22avbWBKGRW5xw15MZo= X-Received: by 2002:a05:6512:3e1a:b0:50e:1412:57e7 with SMTP id i26-20020a0565123e1a00b0050e141257e7mr1433255lfv.8.1702573827713; Thu, 14 Dec 2023 09:10:27 -0800 (PST) Precedence: bulk X-Mailing-List: netfilter-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: =?utf-8?q?G=C3=A9rald_Colangelo?= Date: Thu, 14 Dec 2023 18:10:16 +0100 Message-ID: Subject: Bug in ulogd2 when destroying a stack that failed to start (with fix attached) To: netfilter-devel@vger.kernel.org Hello everyone, While working on a ulogd2 deployment, i built a stack that consists in: - A "home-made" INPUT plugin that launchs a pcap capture upon start() and passes a fd to ulogd2. - Some regular ulogd2 filters - JSON output plugin that writes to a Unix socket. Everything works fine, except if unix socket is not available. In that case, when the stack is started at launch, the JSON output start() function returns -1 and ulogd.c consider that it fails starting the stack and then destroy it. Unfortunately, ulogd.c only free() the stack context without calling stop() functions on plugins that were correctly start()-ed. In my case, it results in my pcap input triggering the stack even if it was destroyed. This ends with a segmentation fault for the ulogd process. I think we should consider calling stop() functions before destroying the stack. A patch implementing this is attached (for version 2.0.7, but this part of the code didn't changed on latest). Best regards, --- ulogd2-2.0.7/src/ulogd.c 2018-04-27 01:10:42.316872034 +0200 +++ ulogd2-2.0.7-patched/src/ulogd.c 2023-12-14 18:04:06.994661722 +0100 @@ -931,7 +931,7 @@ static int create_stack_start_instances(struct ulogd_pluginstance_stack *stack) { int ret; - struct ulogd_pluginstance *pi; + struct ulogd_pluginstance *pi, *stop; /* start from input to output plugin */ llist_for_each_entry(pi, &stack->list, list) { @@ -945,11 +945,27 @@ ulogd_log(ULOGD_ERROR, "error starting `%s'\n", pi->id); - return ret; + goto cleanup_fail; } } } return 0; +cleanup_fail: + stop = pi; + llist_for_each_entry(pi, &stack->list, list) { + if (pi == stop) + /* the one that failed, stops the cleanup here */ + break; + if (!pi->plugin->stop) + continue; + ret = pi->plugin->stop(pi); + if (ret < 0) { + ulogd_log(ULOGD_ERROR, + "error stopping `%s'\n", + pi->id); + } + } + return -1; } /* create a new stack of plugins */