From nobody Wed Nov 12 00:30:43 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1566827901; cv=none; d=zoho.com; s=zohoarc; b=iZ9k0YhkGUxXy68LOGm4EmtYB8DYKZKC/TtLW9fSN84aI7bKWEGn7EgPektaC18hVNJYzzruzmM8pKdT82X1FX+0cERWhbxjBRZOCGCiqxTRROd2atY1Z4KtYWmnZMlkOZpJ2YyQY4L7JSO1QtPDRnVEmIJSZRjI4HaZBl5smzY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1566827901; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=227uxk2qKRq/Sd/4E3fflhN6mK5iV8knH4dhcoftdb0=; b=JW6DGtGOF6NH+Im0ndXK9W8M/9etSlR/kDhXvkRDGgjvq6X4vPfNhaP0erP9OZhc1JeASPutSSNl6jkJhCBh5ULFUkSwWrBDCGIuPDVidfJx28uypORmu96zuxCT9FkFt2ou9oSGQqs811/26T05AXih7ru5fqXwiS22pvHadVs= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1566827901814976.883866028157; Mon, 26 Aug 2019 06:58:21 -0700 (PDT) Received: from localhost ([::1]:53138 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i2FVj-0005DI-Fb for importer@patchew.org; Mon, 26 Aug 2019 09:58:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38031) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i2FPI-00081n-AU for qemu-devel@nongnu.org; Mon, 26 Aug 2019 09:51:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i2FPG-0006Oh-Ti for qemu-devel@nongnu.org; Mon, 26 Aug 2019 09:51:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39588) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i2FOv-0006I5-2z; Mon, 26 Aug 2019 09:51:17 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5D089308FB82; Mon, 26 Aug 2019 13:51:16 +0000 (UTC) Received: from maximlenovopc.usersys.redhat.com (unknown [10.35.206.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id E628A5D6C8; Mon, 26 Aug 2019 13:51:13 +0000 (UTC) From: Maxim Levitsky To: qemu-devel@nongnu.org Date: Mon, 26 Aug 2019 16:50:51 +0300 Message-Id: <20190826135103.22410-2-mlevitsk@redhat.com> In-Reply-To: <20190826135103.22410-1-mlevitsk@redhat.com> References: <20190826135103.22410-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Mon, 26 Aug 2019 13:51:16 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 01/13] introduce g_autowipe X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , qemu-block@nongnu.org, Markus Armbruster , Max Reitz , Stefan Hajnoczi , Maxim Levitsky Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Marking a pointer with g_autowipe, will not only free it at the scope exit, but also erase the data it points to just prior to freeing it. This is first attempt to implement this feature, as suggested by Daniel and Nir. The things that need to be verified prior to merging this is 1. Can we just always use memset_s (defined in C++) or some alternative. 2. is it portable enought for us to use malloc_usable_size to get the size of malloced pointer in the autofree callback? This function is aviable in glibc (but no wrapper in glib). Thanks for Daniel for the g_autowipe and to Nir for the information about the fact that plain memset is usually optimized away. Suggested-by: Daniel P. Berrang=C3=A9 Suggested-by: Nir Soffer Signed-off-by: Maxim Levitsky --- include/autowipe.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 include/autowipe.h diff --git a/include/autowipe.h b/include/autowipe.h new file mode 100644 index 0000000000..1ed4eaf3ba --- /dev/null +++ b/include/autowipe.h @@ -0,0 +1,52 @@ +/* + * g_autowipe implementation for crypto secret wiping + * + * Copyright (c) 2019 Red Hat, Inc. + * Copyright (c) 2019 Maxim Levitsky + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + + +#include +#include +#include + + +/* + * based on + * https://www.cryptologie.net/article/419/zeroing-memory-compiler-optimiz= ations-and-memset_s/ + */ + +static inline void memerase(void *pointer, size_t size) +{ +#ifdef __STDC_LIB_EXT1__ + memset_s(pointer, size, 0, size); +#else + /*volatile used to force compiler to not optimize the code away*/ + volatile unsigned char *p =3D pointer; + while (size--) { + *p++ =3D 0; + } +#endif +} + +static void g_autoptr_cleanup_generic_wipe_gfree(void *p) +{ + void **pp =3D (void **)p; + size_t size =3D malloc_usable_size(*pp); + memerase(*pp, size); + g_free(*pp); +} + +#define g_autowipe _GLIB_CLEANUP(g_autoptr_cleanup_generic_wipe_gfree) --=20 2.17.2