LCOV - code coverage report
Current view: top level - gcr - gcr-secure-memory.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 19 38 50.0 %
Date: 2022-09-04 10:20:22 Functions: 4 8 50.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 8 18 44.4 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2                 :            : /* gcr-secure-memory.c - library for allocating memory that is non-pageable
       3                 :            : 
       4                 :            :    Copyright (C) 2007 Stefan Walter
       5                 :            :    Copyright (C) 2012 Red Hat Inc.
       6                 :            : 
       7                 :            :    The Gnome Keyring Library is free software; you can redistribute it and/or
       8                 :            :    modify it under the terms of the GNU Library General Public License as
       9                 :            :    published by the Free Software Foundation; either version 2 of the
      10                 :            :    License, or (at your option) any later version.
      11                 :            : 
      12                 :            :    The Gnome Keyring Library is distributed in the hope that it will be useful,
      13                 :            :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :    Library General Public License for more details.
      16                 :            : 
      17                 :            :    You should have received a copy of the GNU Library General Public
      18                 :            :    License along with the Gnome Library; see the file COPYING.LIB.  If not,
      19                 :            :    see <http://www.gnu.org/licenses/>.
      20                 :            : 
      21                 :            :    Author: Stef Walter <stefw@gnome.org>
      22                 :            : */
      23                 :            : 
      24                 :            : #include "config.h"
      25                 :            : 
      26                 :            : #include "gcr-secure-memory.h"
      27                 :            : 
      28                 :            : #include "egg/egg-secure-memory.h"
      29                 :            : 
      30                 :            : #include <glib.h>
      31                 :            : 
      32                 :            : #include <string.h>
      33                 :            : 
      34                 :            : /**
      35                 :            :  * gcr_secure_memory_new: (skip)
      36                 :            :  * @type: C type of the objects to allocate
      37                 :            :  * @n_objects: number of objects to allocate
      38                 :            :  *
      39                 :            :  * Allocate objects in non-pageable memory.
      40                 :            :  *
      41                 :            :  * Returns: (transfer full): the new block of memory
      42                 :            :  **/
      43                 :            : 
      44                 :            : /**
      45                 :            :  * gcr_secure_memory_alloc: (skip)
      46                 :            :  * @size: The new desired size of the memory block.
      47                 :            :  *
      48                 :            :  * Allocate a block of non-pageable memory.
      49                 :            :  *
      50                 :            :  * If non-pageable memory cannot be allocated then normal memory will be
      51                 :            :  * returned.
      52                 :            :  *
      53                 :            :  * Return value: (transfer full): new memory block which should be freed
      54                 :            :  * with gcr_secure_memory_free()
      55                 :            :  **/
      56                 :            : gpointer
      57                 :          5 : gcr_secure_memory_alloc (gsize size)
      58                 :            : {
      59                 :            :         gpointer memory;
      60                 :            : 
      61                 :            :         /* Try to allocate secure memory */
      62                 :          5 :         memory = egg_secure_alloc_full ("gcr-secure-memory", size,
      63                 :            :                                         EGG_SECURE_USE_FALLBACK);
      64                 :            : 
      65                 :            :         /* Our fallback will always allocate */
      66         [ -  + ]:          5 :         g_assert (memory != NULL);
      67                 :            : 
      68                 :          5 :         return memory;
      69                 :            : }
      70                 :            : 
      71                 :            : /**
      72                 :            :  * gcr_secure_memory_try_alloc: (skip)
      73                 :            :  * @size: new desired size of the memory block
      74                 :            :  *
      75                 :            :  * Allocate a block of non-pageable memory.
      76                 :            :  *
      77                 :            :  * If non-pageable memory cannot be allocated, then %NULL is returned.
      78                 :            :  *
      79                 :            :  * Return value: (transfer full): new block, or %NULL if memory cannot be
      80                 :            :  * allocated; memory block should be freed with gcr_secure_memory_free()
      81                 :            :  */
      82                 :            : gpointer
      83                 :          0 : gcr_secure_memory_try_alloc (gsize size)
      84                 :            : {
      85                 :          0 :         return egg_secure_alloc_full ("gcr-secure-memory", size, 0);
      86                 :            : }
      87                 :            : 
      88                 :            : /**
      89                 :            :  * gcr_secure_memory_realloc: (skip)
      90                 :            :  * @memory: (nullable): pointer to reallocate or %NULL to allocate a new block
      91                 :            :  * @size: new desired size of the memory block, or 0 to free the memory
      92                 :            :  *
      93                 :            :  * Reallocate a block of non-pageable memory.
      94                 :            :  *
      95                 :            :  * Glib memory is also reallocated correctly. If called with a null pointer,
      96                 :            :  * then a new block of memory is allocated. If called with a zero size,
      97                 :            :  * then the block of memory is freed.
      98                 :            :  *
      99                 :            :  * If non-pageable memory cannot be allocated then normal memory will be
     100                 :            :  * returned.
     101                 :            :  *
     102                 :            :  * Return value: (transfer full): new block, or %NULL if the block was
     103                 :            :  * freed; memory block should be freed with gcr_secure_memory_free()
     104                 :            :  */
     105                 :            : gpointer
     106                 :          5 : gcr_secure_memory_realloc (gpointer memory,
     107                 :            :                            gsize size)
     108                 :            : {
     109                 :            :         gpointer new_memory;
     110                 :            : 
     111         [ +  + ]:          5 :         if (!memory) {
     112                 :          2 :                 return gcr_secure_memory_alloc (size);
     113         [ +  + ]:          3 :         } else if (!size) {
     114                 :          1 :                  gcr_secure_memory_free (memory);
     115                 :          1 :                  return NULL;
     116         [ -  + ]:          2 :         } else if (!egg_secure_check (memory)) {
     117                 :          0 :                 return g_realloc (memory, size);
     118                 :            :         }
     119                 :            : 
     120                 :            :         /* First try and ask secure memory to reallocate */
     121                 :          2 :         new_memory = egg_secure_realloc_full ("gcr-secure-memory", memory,
     122                 :            :                                               size, EGG_SECURE_USE_FALLBACK);
     123                 :            : 
     124         [ -  + ]:          2 :         g_assert (new_memory != NULL);
     125                 :            : 
     126                 :          2 :         return new_memory;
     127                 :            : }
     128                 :            : 
     129                 :            : /**
     130                 :            :  * gcr_secure_memory_try_realloc: (skip)
     131                 :            :  * @memory: (nullable): pointer to reallocate or %NULL to allocate a new block
     132                 :            :  * @size: new desired size of the memory block
     133                 :            :  *
     134                 :            :  * Reallocate a block of non-pageable memory.
     135                 :            :  *
     136                 :            :  * Glib memory is also reallocated correctly when passed to this function.
     137                 :            :  * If called with a null pointer, then a new block of memory is allocated.
     138                 :            :  * If called with a zero size, then the block of memory is freed.
     139                 :            :  *
     140                 :            :  * If memory cannot be allocated, %NULL is returned and the original block
     141                 :            :  * of memory remains intact.
     142                 :            :  *
     143                 :            :  * Return value: (transfer full): the new block, or %NULL if memory cannot be
     144                 :            :  * allocated; the memory block should be freed with gcr_secure_memory_free()
     145                 :            :  */
     146                 :            : gpointer
     147                 :          0 : gcr_secure_memory_try_realloc (gpointer memory,
     148                 :            :                                gsize size)
     149                 :            : {
     150                 :            :         gpointer new_memory;
     151                 :            : 
     152         [ #  # ]:          0 :         if (!memory) {
     153                 :          0 :                 return gcr_secure_memory_try_alloc (size);
     154         [ #  # ]:          0 :         } else if (!size) {
     155                 :          0 :                  gcr_secure_memory_free (memory);
     156                 :          0 :                  return NULL;
     157         [ #  # ]:          0 :         } else if (!egg_secure_check (memory)) {
     158                 :          0 :                 return g_try_realloc (memory, size);
     159                 :            :         }
     160                 :            : 
     161                 :            :         /* First try and ask secure memory to reallocate */
     162                 :          0 :         new_memory = egg_secure_realloc_full ("gcr-secure-memory", memory,
     163                 :            :                                               size, 0);
     164                 :            : 
     165                 :            :         /* Might be NULL if reallocation failed. */
     166                 :          0 :         return new_memory;
     167                 :            : }
     168                 :            : 
     169                 :            : /**
     170                 :            :  * gcr_secure_memory_free: (skip)
     171                 :            :  * @memory: (nullable): pointer to the beginning of the block of memory to free
     172                 :            :  *
     173                 :            :  * Free a block of non-pageable memory.
     174                 :            :  *
     175                 :            :  * Glib memory is also freed correctly when passed to this function. If called
     176                 :            :  * with a %NULL pointer then no action is taken.
     177                 :            :  */
     178                 :            : void
     179                 :          5 : gcr_secure_memory_free (gpointer memory)
     180                 :            : {
     181         [ -  + ]:          5 :         if (!memory)
     182                 :          0 :                 return;
     183                 :          5 :         egg_secure_free_full (memory, EGG_SECURE_USE_FALLBACK);
     184                 :            : }
     185                 :            : 
     186                 :            : /**
     187                 :            :  * gcr_secure_memory_is_secure: (skip)
     188                 :            :  * @memory: pointer to check
     189                 :            :  *
     190                 :            :  * Check if a pointer is in non-pageable memory allocated by.
     191                 :            :  *
     192                 :            :  * Returns: whether the memory is secure non-pageable memory allocated by the
     193                 :            :  *          Gcr library or not
     194                 :            :  */
     195                 :            : gboolean
     196                 :          2 : gcr_secure_memory_is_secure (gpointer memory)
     197                 :            : {
     198                 :          2 :         return egg_secure_check (memory) ? TRUE : FALSE;
     199                 :            : }
     200                 :            : 
     201                 :            : /**
     202                 :            :  * gcr_secure_memory_strdup: (skip)
     203                 :            :  * @string: (nullable): null terminated string to copy
     204                 :            :  *
     205                 :            :  * Copy a string into non-pageable memory. If the input string is %NULL, then
     206                 :            :  * %NULL will be returned.
     207                 :            :  *
     208                 :            :  * Returns: copied string, should be freed with gcr_secure_memory_free()
     209                 :            :  */
     210                 :            : gchar *
     211                 :          0 : gcr_secure_memory_strdup (const gchar* string)
     212                 :            : {
     213                 :          0 :         return egg_secure_strdup_full ("gcr-secure-memory", string,
     214                 :            :                                        EGG_SECURE_USE_FALLBACK);
     215                 :            : }
     216                 :            : 
     217                 :            : /**
     218                 :            :  * gcr_secure_memory_strfree: (skip)
     219                 :            :  * @string: (nullable): null terminated string to fere
     220                 :            :  *
     221                 :            :  * Free a string, whether securely allocated using these functions or not.
     222                 :            :  * This will also clear out the contents of the string so they do not
     223                 :            :  * remain in memory.
     224                 :            :  */
     225                 :            : void
     226                 :          0 : gcr_secure_memory_strfree (gchar *string)
     227                 :            : {
     228                 :          0 :         egg_secure_strfree (string);
     229                 :          0 : }

Generated by: LCOV version 1.14