From patchwork Wed Jun 13 17:18:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phil Sutter X-Patchwork-Id: 928989 X-Patchwork-Delegate: pablo@netfilter.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netfilter-devel-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=nwl.cc Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 415YPV58prz9s01 for ; Thu, 14 Jun 2018 03:18:38 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935400AbeFMRSh (ORCPT ); Wed, 13 Jun 2018 13:18:37 -0400 Received: from orbyte.nwl.cc ([151.80.46.58]:53350 "EHLO orbyte.nwl.cc" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935399AbeFMRSh (ORCPT ); Wed, 13 Jun 2018 13:18:37 -0400 Received: from localhost ([::1]:40394 helo=tatos) by orbyte.nwl.cc with esmtp (Exim 4.90_1) (envelope-from ) id 1fT9Pn-0004x7-43; Wed, 13 Jun 2018 19:18:35 +0200 From: Phil Sutter To: Pablo Neira Ayuso Cc: netfilter-devel@vger.kernel.org Subject: [nft PATCH] libnftables: Fix exit_cookie() Date: Wed, 13 Jun 2018 19:18:24 +0200 Message-Id: <20180613171824.23796-1-phil@nwl.cc> X-Mailer: git-send-email 2.17.0 Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org The output and error buffer feature depends on cookie->orig_fp to indicate the current status of buffering: If it is set, a prior call to init_cookie() is assumed. Though exit_cookie() missed to reset that pointer to NULL. causing weird behaviour in applications if they do: | nft = nft_ctx_new(0); | nft_ctx_buffer_output(nft); | nft_ctx_unbuffer_output(nft); | nft_ctx_buffer_output(nft); While being at it, apply the same fix to error path in init_cookie() as well. Signed-off-by: Phil Sutter --- src/libnftables.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libnftables.c b/src/libnftables.c index 5ee4b8965fc81..760deecf2b899 100644 --- a/src/libnftables.c +++ b/src/libnftables.c @@ -198,6 +198,7 @@ static int init_cookie(struct cookie *cookie) cookie->fp = fopencookie(cookie, "w", cookie_fops); if (!cookie->fp) { cookie->fp = cookie->orig_fp; + cookie->orig_fp = NULL; return 1; } @@ -211,6 +212,7 @@ static int exit_cookie(struct cookie *cookie) fclose(cookie->fp); cookie->fp = cookie->orig_fp; + cookie->orig_fp = NULL; free(cookie->buf); cookie->buf = NULL; cookie->buflen = 0;