liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "selinux_internal.h"
#include "context_internal.h"
 
int setexecfilecon(const char *filename, const char *fallback_type)
{
   char * mycon = NULL, *fcon = NULL, *newcon = NULL;
   context_t con = NULL;
   int rc = 0;
 
   if (is_selinux_enabled() < 1)
       return 0;
 
   rc = getcon(&mycon);
   if (rc < 0)
       goto out;
 
   rc = getfilecon(filename, &fcon);
   if (rc < 0)
       goto out;
 
   rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &newcon);
   if (rc < 0)
       goto out;
 
   if (!strcmp(mycon, newcon)) {
       /* No default transition, use fallback_type for now. */
       rc = -1;
       con = context_new(mycon);
       if (!con)
           goto out;
       if (context_type_set(con, fallback_type))
           goto out;
       freecon(newcon);
       newcon = strdup(context_str(con));
       if (!newcon)
           goto out;
       rc = 0;
   }
 
   rc = setexeccon(newcon);
   if (rc < 0)
       goto out;
      out:
 
   if (rc < 0 && security_getenforce() == 0)
       rc = 0;
 
   context_free(con);
   freecon(newcon);
   freecon(fcon);
   freecon(mycon);
   return rc < 0 ? rc : 0;
}
 
#ifndef DISABLE_RPM
int rpm_execcon(unsigned int verified __attribute__ ((unused)),
       const char *filename, char *const argv[], char *const envp[])
{
   int rc;
 
   rc = setexecfilecon(filename, "rpm_script_t");
   if (rc < 0)
       return rc;
 
   return execve(filename, argv, envp);
}
#endif