Coverage Report

Created: 2025-12-14 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/p11-kit/common/vsock.c
Line
Count
Source
1
/*
2
 * Copyright © 2020 Amazon.com, Inc. or its affiliates.
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions
6
 * are met:
7
 *
8
 *     * Redistributions of source code must retain the above
9
 *       copyright notice, this list of conditions and the
10
 *       following disclaimer.
11
 *     * Redistributions in binary form must reproduce the
12
 *       above copyright notice, this list of conditions and
13
 *       the following disclaimer in the documentation and/or
14
 *       other materials provided with the distribution.
15
 *     * The names of contributors to this software may not be
16
 *       used to endorse or promote products derived from this
17
 *       software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30
 * DAMAGE.
31
 *
32
 * Author: David Woodhouse <dwmw2@infradead.org>
33
 */
34
35
#include "config.h"
36
37
#include "vsock.h"
38
39
#include <limits.h>
40
#include <string.h>
41
#include <stdlib.h>
42
43
#include <sys/types.h>
44
#include <sys/stat.h>
45
#include <fcntl.h>
46
#ifdef OS_UNIX
47
#include <unistd.h>
48
#endif
49
50
#ifdef HAVE_VSOCK
51
#include <sys/socket.h>
52
#include <linux/vm_sockets.h>
53
#include <sys/ioctl.h>
54
#endif
55
56
/* This generic parsing utility doesn't actually require the
57
 * vm_sockets.h header and thus doesn't require conditional
58
 * compiliation... except for this one definition. */
59
#ifndef VMADDR_CID_ANY
60
#define VMADDR_CID_ANY -1U
61
#endif
62
63
bool
64
p11_vsock_parse_addr (const char *target,
65
          unsigned int *cid,
66
          unsigned int *port)
67
0
{
68
0
  bool cid_found = false;
69
0
  bool port_found = false;
70
0
  unsigned long val;
71
0
  char *endptr;
72
73
0
  while (*target) {
74
0
    if (strncmp (target, "cid=", 4) == 0) {
75
0
      val = strtoul(target + 4, &endptr, 0);
76
0
      if (val > UINT_MAX || endptr == target + 4)
77
0
        return false;
78
0
      *cid = val;
79
0
      cid_found = true;
80
0
    } else if (strncmp (target, "port=", 5) == 0) {
81
0
      val = strtoul (target + 5, &endptr, 0);
82
0
      if (val > UINT_MAX || endptr == target + 5)
83
0
        return false;
84
0
      *port = val;
85
0
      port_found = true;
86
0
    } else {
87
0
      return false;
88
0
    }
89
90
0
    target = endptr;
91
0
    if (*target == ';')
92
0
      target++;
93
0
    else if (*target)
94
0
      return false;
95
0
  }
96
97
  /* Port is mandatory */
98
0
  if (!port_found)
99
0
    return false;
100
101
  /* CID is optional, defaulting to VMADDR_CID_ANY */
102
0
  if (!cid_found)
103
0
    *cid = VMADDR_CID_ANY;
104
105
0
  return true;
106
0
}
107
108
bool
109
p11_vsock_get_local_cid (unsigned int *cid)
110
0
{
111
#ifndef HAVE_VSOCK
112
  return false;
113
#else
114
0
  int fd = open ("/dev/vsock", O_RDONLY);
115
0
  int rc;
116
117
0
  if (fd == -1)
118
0
    return false;
119
120
0
  rc = ioctl (fd, IOCTL_VM_SOCKETS_GET_LOCAL_CID, cid, sizeof(*cid));
121
0
  close (fd);
122
123
0
  return (rc == 0);
124
0
#endif
125
0
}