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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| static int security_compute_sid(struct selinux_state *state, u32 ssid, u32 tsid, u16 orig_tclass, u32 specified, const char *objname, u32 *out_sid, bool kern) { struct selinux_policy *policy; struct policydb *policydb; struct sidtab *sidtab; struct class_datum *cladatum = NULL; struct context *scontext, *tcontext, newcontext; struct sidtab_entry *sentry, *tentry; struct avtab_key avkey; struct avtab_datum *avdatum; struct avtab_node *node; u16 tclass; int rc = 0; bool sock;
if (!selinux_initialized (state)) { switch (orig_tclass) { case SECCLASS_PROCESS: *out_sid = ssid; break; default: *out_sid = tsid; break; } goto out; }
context_init (&newcontext);
rcu_read_lock ();
policy = rcu_dereference (state->policy);
if (kern) { tclass = unmap_class (&policy->map, orig_tclass); sock = security_is_socket_class (orig_tclass); } else { tclass = orig_tclass; sock = security_is_socket_class (map_class (&policy->map, tclass)); }
policydb = &policy->policydb; sidtab = policy->sidtab;
sentry = sidtab_search_entry (sidtab, ssid); if (!sentry) { pr_err ("SELinux: % s: unrecognized SID % d\n", __func__, ssid); rc = -EINVAL; goto out_unlock; } tentry = sidtab_search_entry (sidtab, tsid); if (!tentry) { pr_err ("SELinux: % s: unrecognized SID % d\n", __func__, tsid); rc = -EINVAL; goto out_unlock; }
scontext = &sentry->context; tcontext = &tentry->context;
if (tclass && tclass <= policydb->p_classes.nprim) cladatum = policydb->class_val_to_struct [tclass - 1];
switch (specified) { case AVTAB_TRANSITION: case AVTAB_CHANGE: if (cladatum && cladatum->default_user == DEFAULT_TARGET) { newcontext.user = tcontext->user; } else { newcontext.user = scontext->user; } break; case AVTAB_MEMBER: newcontext.user = tcontext->user; break; }
if (cladatum && cladatum->default_role == DEFAULT_SOURCE) { newcontext.role = scontext->role; } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) { newcontext.role = tcontext->role; } else { if ((tclass == policydb->process_class) || sock) newcontext.role = scontext->role; else newcontext.role = OBJECT_R_VAL; }
if (cladatum && cladatum->default_type == DEFAULT_SOURCE) { newcontext.type = scontext->type; } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) { newcontext.type = tcontext->type; } else { if ((tclass == policydb->process_class) || sock) { newcontext.type = scontext->type; } else { newcontext.type = tcontext->type; } }
avkey.source_type = scontext->type; avkey.target_type = tcontext->type; avkey.target_class = tclass; avkey.specified = specified; avdatum = avtab_search (&policydb->te_avtab, &avkey);
if (!avdatum) { node = avtab_search_node (&policydb->te_cond_avtab, &avkey); for (; node; node = avtab_search_node_next (node, specified)) { if (node->key.specified & AVTAB_ENABLED) { avdatum = &node->datum; break; } } }
if (avdatum) { newcontext.type = avdatum->u.data; }
if (objname) filename_compute_type (policydb, &newcontext, scontext->type, tcontext->type, tclass, objname);
if (specified & AVTAB_TRANSITION) { struct role_trans_datum *rtd; struct role_trans_key rtk = { .role = scontext->role, .type = tcontext->type, .tclass = tclass, };
rtd = policydb_roletr_search (policydb, &rtk); if (rtd) newcontext.role = rtd->new_role; }
rc = mls_compute_sid (policydb, scontext, tcontext, tclass, specified, &newcontext, sock); if (rc) goto out_unlock;
if (!policydb_context_isvalid (policydb, &newcontext)) { rc = compute_sid_handle_invalid_context (state, policy, sentry, tentry, tclass, &newcontext); if (rc) goto out_unlock; } rc = sidtab_context_to_sid (sidtab, &newcontext, out_sid); out_unlock: rcu_read_unlock (); context_destroy (&newcontext); out: return rc; }
|