Coverage Report

Created: 2025-07-11 06:21

/src/selinux/libselinux/src/lgetfilecon.c
Line
Count
Source (jump to first uncovered line)
1
#include <unistd.h>
2
#include <fcntl.h>
3
#include <string.h>
4
#include <stdlib.h>
5
#include <errno.h>
6
#include <sys/xattr.h>
7
#include "selinux_internal.h"
8
#include "policy.h"
9
10
int lgetfilecon_raw(const char *path, char ** context)
11
0
{
12
0
  char *buf;
13
0
  ssize_t size;
14
0
  ssize_t ret;
15
16
0
  size = INITCONTEXTLEN + 1;
17
0
  buf = calloc(1, size);
18
0
  if (!buf)
19
0
    return -1;
20
21
0
  ret = lgetxattr(path, XATTR_NAME_SELINUX, buf, size - 1);
22
0
  if (ret < 0 && errno == ERANGE) {
23
0
    char *newbuf;
24
25
0
    size = lgetxattr(path, XATTR_NAME_SELINUX, NULL, 0);
26
0
    if (size < 0)
27
0
      goto out;
28
29
0
    size++;
30
0
    newbuf = realloc(buf, size);
31
0
    if (!newbuf)
32
0
      goto out;
33
34
0
    buf = newbuf;
35
0
    memset(buf, 0, size);
36
0
    ret = lgetxattr(path, XATTR_NAME_SELINUX, buf, size - 1);
37
0
  }
38
0
      out:
39
0
  if (ret == 0) {
40
    /* Re-map empty attribute values to errors. */
41
0
    errno = ENOTSUP;
42
0
    ret = -1;
43
0
  }
44
0
  if (ret < 0)
45
0
    free(buf);
46
0
  else
47
0
    *context = buf;
48
0
  return ret;
49
0
}
50
51
52
int lgetfilecon(const char *path, char ** context)
53
0
{
54
0
  int ret;
55
0
  char * rcontext = NULL;
56
57
0
  *context = NULL;
58
59
0
  ret = lgetfilecon_raw(path, &rcontext);
60
61
0
  if (ret > 0) {
62
0
    ret = selinux_raw_to_trans_context(rcontext, context);
63
0
    freecon(rcontext);
64
0
  }
65
66
0
  if (ret >= 0 && *context)
67
0
    return strlen(*context) + 1;
68
0
  return ret;
69
0
}