LCOV - code coverage report
Current view: top level - gcr - test-secure-memory.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 72 93 77.4 %
Date: 2022-09-04 10:20:22 Functions: 9 9 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 23 58 39.7 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2                 :            : /* unit-test-memory.c: Test memory allocation functionality
       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/gcr-secure-memory.h"
      27                 :            : #include "egg/egg-secure-memory-private.h"
      28                 :            : 
      29                 :            : #include <glib.h>
      30                 :            : 
      31                 :            : #include <errno.h>
      32                 :            : #include <stdlib.h>
      33                 :            : #include <stdio.h>
      34                 :            : #include <string.h>
      35                 :            : #include <sys/resource.h>
      36                 :            : #include <unistd.h>
      37                 :            : #include <sys/types.h>
      38                 :            : 
      39                 :            : #define IS_ZERO -1
      40                 :            : 
      41                 :            : static int
      42                 :          6 : find_non_zero (gpointer mem, gsize len)
      43                 :            : {
      44                 :            :         guchar *b, *e;
      45                 :          6 :         gsize sz = 0;
      46         [ +  + ]:      34057 :         for (b = (guchar*)mem, e = ((guchar*)mem) + len; b != e; ++b, ++sz) {
      47         [ -  + ]:      34051 :                 if (*b != 0x00)
      48                 :          0 :                         return (int)sz;
      49                 :            :         }
      50                 :            : 
      51                 :          6 :         return -1;
      52                 :            : }
      53                 :            : 
      54                 :            : static gsize
      55                 :          2 : get_rlimit_memlock (void)
      56                 :            : {
      57                 :            :         struct rlimit memlock;
      58                 :            : 
      59                 :            :         /* If the test program is running as a privileged user, it is
      60                 :            :          * not possible to set the limit. Skip the test entirely.
      61                 :            :          * FIXME: make this check more accurate, e.g., using capabilities
      62                 :            :          */
      63         [ +  - ]:          2 :         if (getuid () == 0) {
      64                 :          2 :                 g_test_skip ("test cannot run as root");
      65                 :          2 :                 return 0;
      66                 :            :         }
      67                 :            : 
      68         [ #  # ]:          0 :         if (getrlimit (RLIMIT_MEMLOCK, &memlock) != 0)
      69                 :          0 :                 g_error ("getrlimit() failed: %s", strerror (errno));
      70                 :            : 
      71                 :            :         /* Check that the limit is finite, and that if the caller increases its
      72                 :            :          * size by one (to get the first invalid allocation size), things won’t
      73                 :            :          * explode. */
      74         [ #  # ]:          0 :         if (memlock.rlim_cur == RLIM_INFINITY ||
      75         [ #  # ]:          0 :             memlock.rlim_cur >= G_MAXSIZE - 1) {
      76                 :            :                 /* Try reducing the limit to 64KiB. */
      77                 :          0 :                 memlock.rlim_cur = MIN (64 * 1024, memlock.rlim_max);
      78         [ #  # ]:          0 :                 if (setrlimit (RLIMIT_MEMLOCK, &memlock) != 0) {
      79                 :          0 :                         g_test_skip ("setrlimit() failed");
      80                 :          0 :                         return 0;
      81                 :            :                 }
      82                 :            :         }
      83                 :            : 
      84                 :          0 :         return memlock.rlim_cur;
      85                 :            : }
      86                 :            : 
      87                 :            : static void
      88                 :          1 : test_alloc_free (void)
      89                 :            : {
      90                 :            :         gpointer p;
      91                 :            :         gboolean ret;
      92                 :            : 
      93                 :          1 :         p = gcr_secure_memory_alloc (512);
      94         [ -  + ]:          1 :         g_assert (p != NULL);
      95         [ -  + ]:          1 :         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p, 512));
      96                 :            : 
      97                 :          1 :         memset (p, 0x67, 512);
      98                 :            : 
      99                 :          1 :         ret = gcr_secure_memory_is_secure (p);
     100         [ -  + ]:          1 :         g_assert (ret == TRUE);
     101                 :            : 
     102                 :          1 :         gcr_secure_memory_free (p);
     103                 :          1 : }
     104                 :            : 
     105                 :            : static void
     106                 :          1 : test_alloc_two (void)
     107                 :            : {
     108                 :            :         gpointer p, p2;
     109                 :            :         gboolean ret;
     110                 :            : 
     111                 :          1 :         p2 = gcr_secure_memory_alloc (4);
     112         [ -  + ]:          1 :         g_assert(p2 != NULL);
     113         [ -  + ]:          1 :         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p2, 4));
     114                 :            : 
     115                 :          1 :         memset (p2, 0x67, 4);
     116                 :            : 
     117                 :          1 :         p = gcr_secure_memory_alloc (16200);
     118         [ -  + ]:          1 :         g_assert (p != NULL);
     119         [ -  + ]:          1 :         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p, 16200));
     120                 :            : 
     121                 :          1 :         memset (p, 0x67, 16200);
     122                 :            : 
     123                 :          1 :         ret = gcr_secure_memory_is_secure (p);
     124         [ -  + ]:          1 :         g_assert (ret == TRUE);
     125                 :            : 
     126                 :          1 :         gcr_secure_memory_free (p2);
     127                 :          1 :         gcr_secure_memory_free (p);
     128                 :          1 : }
     129                 :            : 
     130                 :            : /* Test alloc() with an allocation larger than RLIMIT_MEMLOCK, which should
     131                 :            :  * fail. */
     132                 :            : static void
     133                 :          1 : test_alloc_oversized (void)
     134                 :            : {
     135                 :            :         gsize limit;
     136                 :            :         gpointer mem;
     137                 :            : 
     138                 :          1 :         limit = get_rlimit_memlock ();
     139         [ +  - ]:          1 :         if (limit == 0)
     140                 :          1 :                 return;
     141                 :            : 
     142                 :            :         /* Try the allocation. */
     143                 :          0 :         egg_set_secure_warnings (0);
     144                 :            : 
     145                 :          0 :         mem = gcr_secure_memory_try_alloc (limit + 1);
     146         [ #  # ]:          0 :         g_assert_null (mem);
     147                 :            : 
     148                 :          0 :         egg_set_secure_warnings (1);
     149                 :            : }
     150                 :            : 
     151                 :            : static void
     152                 :          1 : test_realloc (void)
     153                 :            : {
     154                 :          1 :         const gchar *str = "a test string to see if realloc works properly";
     155                 :            :         gpointer p, p2;
     156                 :            :         gsize len;
     157                 :            : 
     158                 :          1 :         len = strlen (str) + 1;
     159                 :            : 
     160                 :          1 :         p = gcr_secure_memory_realloc (NULL, len);
     161         [ -  + ]:          1 :         g_assert (p != NULL);
     162         [ -  + ]:          1 :         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p, len));
     163                 :            : 
     164                 :          1 :         strcpy ((gchar*)p, str);
     165                 :            : 
     166                 :          1 :         p2 = gcr_secure_memory_realloc (p, 512);
     167         [ -  + ]:          1 :         g_assert (p2 != NULL);
     168                 :            : 
     169                 :            :         /* "strings not equal after realloc" */
     170         [ -  + ]:          1 :         g_assert_cmpstr (p2, ==, str);
     171                 :            : 
     172                 :          1 :         p = gcr_secure_memory_realloc (p2, 0);
     173                 :            :         /* "should have freed memory" */
     174         [ -  + ]:          1 :         g_assert (p == NULL);
     175                 :          1 : }
     176                 :            : 
     177                 :            : static void
     178                 :          1 : test_realloc_across (void)
     179                 :            : {
     180                 :            :         gpointer p, p2;
     181                 :            : 
     182                 :            :         /* Tiny allocation */
     183                 :          1 :         p = gcr_secure_memory_realloc (NULL, 1088);
     184         [ -  + ]:          1 :         g_assert (p != NULL);
     185         [ -  + ]:          1 :         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p, 1088));
     186                 :            : 
     187                 :            :         /* Reallocate to a large one, will have to have changed blocks */
     188                 :          1 :         p2 = gcr_secure_memory_realloc (p, 16200);
     189         [ -  + ]:          1 :         g_assert (p2 != NULL);
     190         [ -  + ]:          1 :         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p2, 16200));
     191                 :            : 
     192                 :          1 :         gcr_secure_memory_free (p2);
     193                 :          1 : }
     194                 :            : 
     195                 :            : /* Test realloc() with an allocation larger than RLIMIT_MEMLOCK, which should
     196                 :            :  * fail. */
     197                 :            : static void
     198                 :          1 : test_realloc_oversized (void)
     199                 :            : {
     200                 :            :         gsize limit;
     201                 :            :         gpointer mem, new_mem;
     202                 :            : 
     203                 :          1 :         limit = get_rlimit_memlock ();
     204         [ +  - ]:          1 :         if (limit == 0)
     205                 :          1 :                 return;
     206                 :            : 
     207                 :            :         /* Try the allocation. */
     208                 :          0 :         mem = gcr_secure_memory_alloc (64);
     209         [ #  # ]:          0 :         g_assert_nonnull (mem);
     210                 :            : 
     211                 :          0 :         egg_set_secure_warnings (0);
     212                 :            : 
     213                 :          0 :         new_mem = gcr_secure_memory_try_realloc (mem, limit + 1);
     214         [ #  # ]:          0 :         g_assert_null (new_mem);
     215                 :            : 
     216                 :          0 :         egg_set_secure_warnings (1);
     217                 :            : 
     218                 :          0 :         gcr_secure_memory_free (mem);
     219                 :            : }
     220                 :            : 
     221                 :            : int
     222                 :          1 : main (int argc, char **argv)
     223                 :            : {
     224                 :          1 :         g_test_init (&argc, &argv, NULL);
     225                 :          1 :         g_set_prgname ("test-memory");
     226                 :            : 
     227                 :          1 :         g_test_add_func ("/memory/alloc-free", test_alloc_free);
     228                 :          1 :         g_test_add_func ("/memory/alloc-two", test_alloc_two);
     229                 :          1 :         g_test_add_func ("/memory/alloc-oversized", test_alloc_oversized);
     230                 :          1 :         g_test_add_func ("/memory/realloc", test_realloc);
     231                 :          1 :         g_test_add_func ("/memory/realloc-across", test_realloc_across);
     232                 :          1 :         g_test_add_func ("/memory/realloc-oversized", test_realloc_oversized);
     233                 :            : 
     234                 :          1 :         return g_test_run ();
     235                 :            : }

Generated by: LCOV version 1.14