From patchwork Wed Sep 16 16:21:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Richter X-Patchwork-Id: 1365443 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Bs52v41w3z9sTq for ; Thu, 17 Sep 2020 02:21:59 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linux.ibm.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=qWyhBbv4; dkim-atps=neutral Received: from bilbo.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 4Bs52v1pRqzDqNs for ; Thu, 17 Sep 2020 02:21:59 +1000 (AEST) X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=linux.ibm.com (client-ip=148.163.158.5; helo=mx0a-001b2d01.pphosted.com; envelope-from=erichte@linux.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=qWyhBbv4; dkim-atps=neutral Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4Bs52h5CD4zDqMM for ; Thu, 17 Sep 2020 02:21:48 +1000 (AEST) Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 08GGG3PQ077223 for ; Wed, 16 Sep 2020 12:21:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=zebUlG9CqF8kwGZ/svW05sy6TehIFoSEogqs/huFTC4=; b=qWyhBbv48LKJkah+qCZAuKleZ51mDl6EIJhRYrQzMbMW7FRZywbeCG8YIjvHEYsODUIB ZkCkjM3rCP3HzhFX65C6z70RLcgfnB+TaTMRA4CCJTSWhEBM99bKo2iMBnn+Yj6AXxAH Ct7Ye8kQdQcZLOu9S9em3eoppQMD+CdApYVauQQdvpBXy9bP9e0NWS2js0rsPFyZqxl0 NRZVWoNVVrp1OTycfq7l2tAVuO+800QPWA9zRNI2l5+joTlNomn0g5P29yGex5o37qnt 73p98zzPd3qY4uNUzKCIsAfXo50lS5SDQtS/FWTl5O44PMfmoMFuwFG82cRuANyWNJt7 GQ== Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0b-001b2d01.pphosted.com with ESMTP id 33kp30054m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 16 Sep 2020 12:21:46 -0400 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 08GGIm5J017718 for ; Wed, 16 Sep 2020 16:21:44 GMT Received: from b06avi18626390.portsmouth.uk.ibm.com (b06avi18626390.portsmouth.uk.ibm.com [9.149.26.192]) by ppma04ams.nl.ibm.com with ESMTP id 33k5up8urr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 16 Sep 2020 16:21:44 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06avi18626390.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 08GGK7ve34537966 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 16 Sep 2020 16:20:07 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CED5D11C054; Wed, 16 Sep 2020 16:21:41 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2C63A11C050; Wed, 16 Sep 2020 16:21:41 +0000 (GMT) Received: from ceres.ibmuc.com (unknown [9.160.109.67]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Wed, 16 Sep 2020 16:21:41 +0000 (GMT) From: Eric Richter To: skiboot@lists.ozlabs.org Date: Wed, 16 Sep 2020 11:21:17 -0500 Message-Id: <20200916162131.22478-7-erichte@linux.ibm.com> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200916162131.22478-1-erichte@linux.ibm.com> References: <20200916162131.22478-1-erichte@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235, 18.0.687 definitions=2020-09-16_10:2020-09-16, 2020-09-16 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 mlxscore=0 lowpriorityscore=0 suspectscore=3 clxscore=1015 mlxlogscore=516 phishscore=0 priorityscore=1501 malwarescore=0 spamscore=0 bulkscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2009160114 Subject: [Skiboot] [PATCH v6 06/20] secvar_util: add new helper functions X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: nayna@linux.ibm.com Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" This patch adds the following helper functions: - dealloc_secvar() - new_secvar() - copy_bank_list() dealloc_secvar() frees a whole secvar_node reference including its children allocations. This also updates the clear_bank_list() helper function to use this destructor. new_secvar() allocates a secvar_node, and fills it with data provided via arguments. copy_bank_list() creates a deep copy of a secvar bank list Signed-off-by: Eric Richter --- libstb/secvar/secvar.h | 5 +++ libstb/secvar/secvar_util.c | 63 +++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/libstb/secvar/secvar.h b/libstb/secvar/secvar.h index a0cafbb0..b141b705 100644 --- a/libstb/secvar/secvar.h +++ b/libstb/secvar/secvar.h @@ -43,8 +43,13 @@ extern struct secvar_backend_driver secvar_backend; // Helper functions void clear_bank_list(struct list_head *bank); +int copy_bank_list(struct list_head *dst, struct list_head *src); struct secvar_node *alloc_secvar(uint64_t size); +struct secvar_node *new_secvar(const char *key, uint64_t key_len, + const char *data, uint64_t data_size, + uint64_t flags); int realloc_secvar(struct secvar_node *node, uint64_t size); +void dealloc_secvar(struct secvar_node *node); struct secvar_node *find_secvar(const char *key, uint64_t key_len, struct list_head *bank); int is_key_empty(const char *key, uint64_t key_len); int list_length(struct list_head *bank); diff --git a/libstb/secvar/secvar_util.c b/libstb/secvar/secvar_util.c index 2005de58..3aadbd98 100644 --- a/libstb/secvar/secvar_util.c +++ b/libstb/secvar/secvar_util.c @@ -20,11 +20,31 @@ void clear_bank_list(struct list_head *bank) list_for_each_safe(bank, node, next, link) { list_del(&node->link); + dealloc_secvar(node); + } +} + +int copy_bank_list(struct list_head *dst, struct list_head *src) +{ + struct secvar_node *node, *tmp; - if (node->var) - free(node->var); - free(node); + list_for_each(src, node, link) { + /* Allocate new secvar using actual data size */ + tmp = alloc_secvar(node->var->data_size); + if (!tmp) + return OPAL_NO_MEM; + + /* Copy over flags metadata */ + tmp->flags = node->flags; + + /* Full-clone over the secvar struct */ + memcpy(tmp->var, node->var, tmp->size + sizeof(struct secvar)); + + /* Append to new list */ + list_add_tail(dst, &tmp->link); } + + return OPAL_SUCCESS; } struct secvar_node *alloc_secvar(uint64_t size) @@ -46,6 +66,34 @@ struct secvar_node *alloc_secvar(uint64_t size) return ret; } +struct secvar_node *new_secvar(const char *key, uint64_t key_len, + const char *data, uint64_t data_size, + uint64_t flags) +{ + struct secvar_node *ret; + + if (!key) + return NULL; + if ((!key_len) || (key_len > SECVAR_MAX_KEY_LEN)) + return NULL; + if ((!data) && (data_size)) + return NULL; + + ret = alloc_secvar(data_size); + if (!ret) + return NULL; + + ret->var->key_len = key_len; + ret->var->data_size = data_size; + memcpy(ret->var->key, key, key_len); + ret->flags = flags; + + if (data) + memcpy(ret->var->data, data, data_size); + + return ret; +} + int realloc_secvar(struct secvar_node *node, uint64_t size) { void *tmp; @@ -64,6 +112,15 @@ int realloc_secvar(struct secvar_node *node, uint64_t size) return 0; } +void dealloc_secvar(struct secvar_node *node) +{ + if (!node) + return; + + free(node->var); + free(node); +} + struct secvar_node *find_secvar(const char *key, uint64_t key_len, struct list_head *bank) { struct secvar_node *node = NULL;