namespace.c 17.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Y
yangwenjun 已提交
16 17
#include "namespace.h"

W
wangjiahui 已提交
18 19 20
#include "ld_log.h"
#include "strops.h"

Y
yangwenjun 已提交
21 22 23
static ns_t g_ns_default;
static nslist g_ns_list;

24
#ifndef NSLIST_DEFAULT_SIZE
Y
yangwenjun 已提交
25
#define NSLIST_DEFAULT_SIZE 16
26 27
#endif
#ifndef DSOLIST_DEFAULT_SIZE
Y
yangwenjun 已提交
28
#define DSOLIST_DEFAULT_SIZE 16
29 30
#endif
#ifndef INHERIT_DEFAULT_SIZE
Y
yangwenjun 已提交
31
#define INHERIT_DEFAULT_SIZE 16
32
#endif
Y
yangwenjun 已提交
33

D
dhy308 已提交
34 35 36 37 38 39
#ifdef UNIT_TEST_STATIC
    #define UT_STATIC
#else
    #define UT_STATIC static
#endif

Y
yinchuang 已提交
40 41
#define ALLOW_ALL_SHARED_LIBS "allow_all_shared_libs"

Y
yangwenjun 已提交
42 43 44
static ns_inherit_list *nsinherits_alloc()
{
    ns_inherit_list *nsinl;
D
dhy308 已提交
45
    nsinl = (ns_inherit_list *)__libc_calloc(1, sizeof *nsinl);
Y
yangwenjun 已提交
46 47 48

    if (nsinl) {
        nsinl->size = INHERIT_DEFAULT_SIZE;
D
dhy308 已提交
49
        nsinl->inherits = (ns_inherit **)__libc_calloc(INHERIT_DEFAULT_SIZE, sizeof *nsinl->inherits);
Y
yangwenjun 已提交
50
        if (!nsinl->inherits) {
G
ganlan 已提交
51
            LD_LOGE("nsinherits_alloc failed,return NULL!");
D
dhy308 已提交
52
            __libc_free(nsinl);
Y
yangwenjun 已提交
53 54 55 56 57 58 59 60
            nsinl = NULL;
        }
    }
    return nsinl;
}

static void nsinherits_free(ns_inherit_list *nsinl)
{
W
wangjiahui 已提交
61 62 63
    if (!nsinl) {
        return;
    }
G
ganlan 已提交
64
    for (size_t i = 0; i < nsinl->num; i++) {
Y
yangwenjun 已提交
65
        strlist_free(nsinl->inherits[i]->shared_libs);
D
dhy308 已提交
66
        __libc_free(nsinl->inherits[i]);
Y
yangwenjun 已提交
67
    }
D
dhy308 已提交
68 69
    __libc_free(nsinl->inherits);
    __libc_free(nsinl);
Y
yangwenjun 已提交
70 71
}

D
dhy308 已提交
72
UT_STATIC void nsinherits_realloc(ns_inherit_list *nsinl)
Y
yangwenjun 已提交
73
{
W
wangjiahui 已提交
74 75 76
    if (!nsinl) {
        return;
    }
G
ganlan 已提交
77
    size_t size = 2 * nsinl->size;
Y
yangwenjun 已提交
78 79
    if (size) {
        ns_inherit **inherits;
D
dhy308 已提交
80
        inherits = (ns_inherit **)__libc_realloc(nsinl->inherits, size * (sizeof *nsinl->inherits));
W
wangjiahui 已提交
81
        if (!inherits) {
G
ganlan 已提交
82
            LD_LOGE("nsinherits_realloc failed!");
W
wangjiahui 已提交
83 84
            return;
        }
Y
yangwenjun 已提交
85 86 87 88 89 90 91 92 93
        nsinl->size = size;
        nsinl->inherits = inherits;
    }
    return;
}

static dsolist *dsolist_alloc()
{
    dsolist *dsol;
D
dhy308 已提交
94
    dsol = (dsolist *)__libc_calloc(1, sizeof *dsol);
Y
yangwenjun 已提交
95 96

    if (dsol) {
G
ganlan 已提交
97
        dsol->size = DSOLIST_DEFAULT_SIZE;
D
dhy308 已提交
98
        dsol->dsos = (struct dso **)__libc_calloc(DSOLIST_DEFAULT_SIZE, sizeof *dsol->dsos);
Y
yangwenjun 已提交
99
        if (!dsol->dsos) {
G
ganlan 已提交
100
            LD_LOGE("dsolist_alloc failed,return NULL!");
D
dhy308 已提交
101
            __libc_free(dsol);
Y
yangwenjun 已提交
102 103 104
            dsol = NULL;
        }
    }
G
ganlan 已提交
105
    return dsol;
Y
yangwenjun 已提交
106 107 108 109
}

static void dsolist_realloc(dsolist *dsol)
{
W
wangjiahui 已提交
110 111 112
    if (!dsol) {
        return;
    }
G
ganlan 已提交
113
    size_t size = 2 * dsol->size;
Y
yangwenjun 已提交
114 115
    if (size) {
        struct dso **ds;
D
dhy308 已提交
116
        ds = (struct dso **)__libc_realloc(dsol->dsos, size * (sizeof *dsol->dsos));
W
wangjiahui 已提交
117
        if (!ds) {
G
ganlan 已提交
118
            LD_LOGE("dsolist_realloc failed!");
W
wangjiahui 已提交
119 120
            return;
        }
Y
yangwenjun 已提交
121 122 123 124 125 126 127 128
        dsol->size = size;
        dsol->dsos = ds;
    }
    return;
}

ns_t *ns_alloc()
{
D
dhy308 已提交
129
    ns_t *nst = (ns_t *)__libc_calloc(1, sizeof *nst);
Y
yangwenjun 已提交
130 131
    nst->ns_dsos = dsolist_alloc();
    if (!nst->ns_dsos) {
G
ganlan 已提交
132
        LD_LOGE("ns_alloc failed,return NULL!");
D
dhy308 已提交
133
        __libc_free(nst);
Y
yangwenjun 已提交
134 135 136 137 138 139 140
        nst = NULL;
    }
    return nst;
}

void ns_free(ns_t *ns)
{
W
wangjiahui 已提交
141 142 143
    if (!ns) {
        return;
    }
144
    if (ns->ns_dsos) {
D
dhy308 已提交
145
        __libc_free(ns->ns_dsos);
146 147 148
        ns->ns_dsos = NULL;
    }
    if (ns->ns_name) {
D
dhy308 已提交
149
        __libc_free(ns->ns_name);
150 151 152
        ns->ns_name = NULL;
    }
    if (ns->env_paths) {
D
dhy308 已提交
153
        __libc_free(ns->env_paths);
154 155 156
        ns->env_paths = NULL;
    }
    if (ns->lib_paths) {
D
dhy308 已提交
157
        __libc_free(ns->lib_paths);
158 159 160
        ns->lib_paths = NULL;
    }
    if (ns->asan_lib_paths) {
D
dhy308 已提交
161
        __libc_free(ns->asan_lib_paths);
162 163
        ns->asan_lib_paths = NULL;
    }
Y
yangwenjun 已提交
164
    strlist_free(ns->permitted_paths);
W
wangjiahui 已提交
165
    strlist_free(ns->asan_permitted_paths);
Y
yangwenjun 已提交
166 167
    strlist_free(ns->allowed_libs);
    nsinherits_free(ns->ns_inherits);
D
dhy308 已提交
168
    __libc_free(ns);
Y
yangwenjun 已提交
169 170 171 172
}

void ns_add_dso(ns_t *ns, struct dso *dso)
{
G
ganlan 已提交
173
    if (!ns || !dso) {
W
wangjiahui 已提交
174 175
        return;
    }
Y
yangwenjun 已提交
176 177 178
    if (!ns->ns_dsos) {
        ns->ns_dsos = dsolist_alloc();
    }
W
wangjiahui 已提交
179 180 181
    if (!ns->ns_dsos) {
        return;
    }
Y
yangwenjun 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    if (ns->ns_dsos->num == ns->ns_dsos->size) {
        /* if list is full, realloc size to double*/
        dsolist_realloc(ns->ns_dsos);
    }
    if (ns->ns_dsos->num < ns->ns_dsos->size) {
        /* realloc succ */
        ns->ns_dsos->dsos[ns->ns_dsos->num] = dso;
        ns->ns_dsos->num++;
    }
    return;
}

nslist *nslist_init()
{
    g_ns_list.size = NSLIST_DEFAULT_SIZE;
    g_ns_list.num = 0;
D
dhy308 已提交
198
    g_ns_list.nss = (ns_t **)__libc_calloc(NSLIST_DEFAULT_SIZE, sizeof *g_ns_list.nss);
Y
yangwenjun 已提交
199
    if (!g_ns_list.nss) {
G
ganlan 已提交
200
        LD_LOGE("nslist_init failed!");
Y
yangwenjun 已提交
201 202 203 204 205 206 207
        return NULL;
    }
    return &g_ns_list;
}

static void nslist_realloc()
{
G
ganlan 已提交
208
    size_t size = 2 * g_ns_list.size;
Y
yangwenjun 已提交
209 210
    if (size) {
        ns_t **nss;
D
dhy308 已提交
211
        nss = (ns_t **)__libc_realloc(g_ns_list.nss, size * (sizeof *g_ns_list.nss));
W
wangjiahui 已提交
212
        if (!nss) {
G
ganlan 已提交
213
            LD_LOGE("nslist_realloc failed!");
W
wangjiahui 已提交
214 215
            return;
        }
Y
yangwenjun 已提交
216 217 218 219 220 221 222 223
        g_ns_list.size = size;
        g_ns_list.nss = nss;
    }
    return;
}

void nslist_add_ns(ns_t *ns)
{
W
wangjiahui 已提交
224 225 226
    if (!ns) {
        return;
    }
G
ganlan 已提交
227

Y
yangwenjun 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    if (g_ns_list.num == g_ns_list.size) {
        /* if list is full, realloc size to double*/
        nslist_realloc();
    }
    if (g_ns_list.num < g_ns_list.size) {
        /* realloc succ */
        g_ns_list.nss[g_ns_list.num] = ns;
        g_ns_list.num++;
    }
    return;
}

ns_t *get_default_ns()
{
    return &g_ns_default;
}

/* set namespace attributes*/
void ns_set_name(ns_t *ns, const char *name)
{
W
wangjiahui 已提交
248 249 250
    if (!ns || !name) {
        return;
    }
D
dhy308 已提交
251
    if (ns->ns_name) __libc_free(ns->ns_name);
252
    ns->ns_name = ld_strdup(name);
Y
yangwenjun 已提交
253
    strtrim(ns->ns_name);
G
ganlan 已提交
254
    LD_LOGD("ns_set_name ns_name:%{public}s.", ns->ns_name);
Y
yangwenjun 已提交
255 256 257 258
}

void ns_set_env_paths(ns_t *ns, const char *env_paths)
{
W
wangjiahui 已提交
259 260 261
    if (!ns) {
        return;
    }
D
dhy308 已提交
262
    if (ns->env_paths) __libc_free(ns->env_paths);
Y
yangwenjun 已提交
263
    if (env_paths) {
264
        ns->env_paths = ld_strdup(env_paths);
Y
yangwenjun 已提交
265 266 267 268
        strtrim(ns->env_paths);
    } else {
        ns->env_paths = NULL;
    }
G
ganlan 已提交
269
    LD_LOGD("ns_set_env_paths ns[%{public}s] env_paths:%{public}s.", ns->ns_name, ns->env_paths);
Y
yangwenjun 已提交
270 271 272 273
}

void ns_set_lib_paths(ns_t *ns, const char *lib_paths)
{
W
wangjiahui 已提交
274 275 276
    if (!ns) {
        return;
    }
D
dhy308 已提交
277
    if (ns->lib_paths) __libc_free(ns->lib_paths);
Y
yangwenjun 已提交
278
    if (lib_paths) {
279
        ns->lib_paths = ld_strdup(lib_paths);
Y
yangwenjun 已提交
280 281 282 283
        strtrim(ns->lib_paths);
    } else {
        ns->lib_paths = NULL;
    }
G
ganlan 已提交
284
    LD_LOGD("ns_set_lib_paths ns[%{public}s] lib_paths:%{public}s.", ns->ns_name, ns->lib_paths);
Y
yangwenjun 已提交
285 286
}

W
wangjiahui 已提交
287
void ns_set_asan_lib_paths(ns_t *ns, const char *asan_lib_paths)
Y
yangwenjun 已提交
288
{
W
wangjiahui 已提交
289 290 291 292
    if (!ns) {
        return;
    }
    if (ns->asan_lib_paths) {
D
dhy308 已提交
293
        __libc_free(ns->asan_lib_paths);
W
wangjiahui 已提交
294 295
    }
    if (asan_lib_paths) {
296
        ns->asan_lib_paths = ld_strdup(asan_lib_paths);
W
wangjiahui 已提交
297 298 299 300
        strtrim(ns->asan_lib_paths);
    } else {
        ns->asan_lib_paths = NULL;
    }
G
ganlan 已提交
301
    LD_LOGD("ns_set_asan_lib_paths ns[%{public}s] asan_lib_paths:%{public}s.", ns->ns_name, ns->asan_lib_paths);
W
wangjiahui 已提交
302 303 304 305 306 307 308
}

void ns_set_permitted_paths(ns_t *ns, const char *permitted_paths)
{
    if (!ns) {
        return;
    }
Y
yangwenjun 已提交
309
    if (ns->permitted_paths) strlist_free(ns->permitted_paths);
G
ganlan 已提交
310 311
    ns->permitted_paths = strsplit(permitted_paths, ":");
    LD_LOGD("ns_set_permitted_paths ns[%{public}s] permitted_paths:%{public}s.", ns->ns_name, permitted_paths);
Y
yangwenjun 已提交
312 313
}

W
wangjiahui 已提交
314
void ns_set_asan_permitted_paths(ns_t *ns, const char *asan_permitted_paths)
Y
yangwenjun 已提交
315
{
W
wangjiahui 已提交
316 317 318 319 320 321 322
    if (!ns) {
        return;
    }
    if (ns->asan_permitted_paths) {
        strlist_free(ns->asan_permitted_paths);
    }
    ns->asan_permitted_paths = strsplit(asan_permitted_paths, ":");
G
ganlan 已提交
323 324 325
    LD_LOGD("ns_set_asan_permitted_paths ns[%{public}s] asan_permitted_paths:%{public}s.",
        ns->ns_name,
        asan_permitted_paths);
W
wangjiahui 已提交
326 327 328 329 330 331 332
}

void ns_set_separated(ns_t *ns, bool separated)
{
    if (!ns) {
        return;
    }
Y
yangwenjun 已提交
333
    ns->separated = separated;
G
ganlan 已提交
334
    LD_LOGD("ns_set_separated ns[%{public}s] separated:%{public}d.", ns->ns_name, ns->separated);
Y
yangwenjun 已提交
335 336 337 338
}

void ns_set_allowed_libs(ns_t *ns, const char *allowed_libs)
{
W
wangjiahui 已提交
339 340 341
    if (!ns) {
        return;
    }
G
ganlan 已提交
342

Y
yangwenjun 已提交
343 344 345 346
    if (ns->allowed_libs) strlist_free(ns->allowed_libs);
    ns->allowed_libs = NULL;
    if (allowed_libs) {
        /* if setted and not empty, split to list. */
347
        char *a_libs = ld_strdup(allowed_libs);
G
ganlan 已提交
348
        if (strtrim(a_libs) > 0) ns->allowed_libs = strsplit(a_libs, ":");
D
dhy308 已提交
349
        __libc_free(a_libs);
Y
yangwenjun 已提交
350
    }
G
ganlan 已提交
351
    LD_LOGD("ns_set_allowed_libs ns[%{public}s] allowed_libs:%{public}s.", ns->ns_name, allowed_libs);
Y
yangwenjun 已提交
352 353 354 355
}

ns_t *find_ns_by_name(const char *ns_name)
{
W
wangjiahui 已提交
356 357 358
    if (!ns_name) {
        return NULL;
    }
G
ganlan 已提交
359 360
    if (strcmp(NS_DEFAULT_NAME, ns_name) == 0) {
        LD_LOGD("find_ns_by_name return default namespace!");
W
wangjiahui 已提交
361 362
        return get_default_ns();
    }
G
ganlan 已提交
363 364
    for (size_t i = 0; i < g_ns_list.num; i++) {
        if (strcmp(g_ns_list.nss[i]->ns_name, ns_name) == 0) {
Y
yangwenjun 已提交
365 366 367
            return g_ns_list.nss[i];
        }
    }
G
ganlan 已提交
368
    LD_LOGD("find_ns_by_name ns_name[%{public}s] failed,return NULL!", ns_name);
Y
yangwenjun 已提交
369 370 371 372 373
    return NULL;
}

static ns_inherit *find_ns_inherit(ns_t *ns, ns_t *inherited)
{
G
ganlan 已提交
374
    if (!ns || !inherited) {
W
wangjiahui 已提交
375 376
        return NULL;
    }
Y
yangwenjun 已提交
377
    if (ns->ns_inherits) {
G
ganlan 已提交
378
        for (size_t i = 0; i < ns->ns_inherits->num; i++) {
Y
yangwenjun 已提交
379 380 381
            if (ns->ns_inherits->inherits[i]->inherited_ns == inherited) return ns->ns_inherits->inherits[i];
        }
    }
G
ganlan 已提交
382 383
    LD_LOGD(
        "find_ns_inherit ns[%{public}s] ns_inherited[%{public}s] failed,return NULL!", ns->ns_name, inherited->ns_name);
Y
yangwenjun 已提交
384 385 386 387
    return NULL;
}

void ns_add_inherit(ns_t *ns, ns_t *ns_inherited, const char *shared_libs)
G
ganlan 已提交
388
{
Y
yangwenjun 已提交
389
    bool need_add = false;
G
ganlan 已提交
390
    if (!ns || !ns_inherited) {
W
wangjiahui 已提交
391 392
        return;
    }
Y
yangwenjun 已提交
393 394 395

    ns_inherit *inherit = find_ns_inherit(ns, ns_inherited);
    if (!inherit) {
D
dhy308 已提交
396
        inherit = __libc_calloc(1, sizeof *inherit);
W
wangjiahui 已提交
397
        if (!inherit) {
G
ganlan 已提交
398 399 400
            LD_LOGE("ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] calloc failed!",
                ns->ns_name,
                ns_inherited->ns_name);
W
wangjiahui 已提交
401 402
            return;
        }
Y
yangwenjun 已提交
403 404
        inherit->inherited_ns = ns_inherited;
        need_add = true;
G
ganlan 已提交
405 406 407
        LD_LOGD("ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] need_add is true.",
            ns->ns_name,
            ns_inherited->ns_name);
Y
yangwenjun 已提交
408
    }
G
ganlan 已提交
409

Y
yangwenjun 已提交
410 411 412 413 414 415 416
    if (inherit->shared_libs) {
        strlist_free(inherit->shared_libs);
        inherit->shared_libs = NULL;
    }

    /* if setted and not empty, split to list. */
    if (shared_libs) {
417
        char *s_libs = ld_strdup(shared_libs);
G
ganlan 已提交
418
        if (strtrim(s_libs) > 0) inherit->shared_libs = strsplit(shared_libs, ":");
D
dhy308 已提交
419
        __libc_free(s_libs);
Y
yangwenjun 已提交
420 421
    }

W
wangjiahui 已提交
422
    if (!need_add) {
G
ganlan 已提交
423 424
        LD_LOGD(
            "ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] not need_add!", ns->ns_name, ns_inherited->ns_name);
W
wangjiahui 已提交
425 426
        return;
    }
Y
yangwenjun 已提交
427 428 429 430 431 432 433

    if (!ns->ns_inherits) {
        ns->ns_inherits = nsinherits_alloc();
    }

    if (!ns->ns_inherits) {
        if (inherit->shared_libs) strlist_free(inherit->shared_libs);
G
ganlan 已提交
434 435 436
        LD_LOGD("ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] nsinherits_alloc failed!",
            ns->ns_name,
            ns_inherited->ns_name);
D
dhy308 已提交
437
        __libc_free(inherit);
Y
yangwenjun 已提交
438 439 440 441 442
        return;
    }

    if (ns->ns_inherits->num == ns->ns_inherits->size) {
        /* if list is full, realloc size to double*/
G
ganlan 已提交
443 444 445
        LD_LOGD("ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] list is full, realloc size to double!",
            ns->ns_name,
            ns_inherited->ns_name);
Y
yangwenjun 已提交
446 447 448 449 450
        nsinherits_realloc(ns->ns_inherits);
    }

    if (ns->ns_inherits->num < ns->ns_inherits->size) {
        /* realloc succ */
G
ganlan 已提交
451 452 453
        LD_LOGD("ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] realloc success!",
            ns->ns_name,
            ns_inherited->ns_name);
Y
yangwenjun 已提交
454 455 456 457
        ns->ns_inherits->inherits[ns->ns_inherits->num] = inherit;
        ns->ns_inherits->num++;
    } else {
        /* realloc failed */
G
ganlan 已提交
458 459 460
        LD_LOGD("ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] realloc failed!",
            ns->ns_name,
            ns_inherited->ns_name);
Y
yangwenjun 已提交
461
        if (inherit->shared_libs) strlist_free(inherit->shared_libs);
D
dhy308 已提交
462
        __libc_free(inherit);
Y
yangwenjun 已提交
463 464 465 466 467
    }
    return;
}

/* check library's pathname if accessible in this namespace */
W
wangjiahui 已提交
468
bool is_accessible(ns_t *ns, const char *lib_pathname, bool is_asan, bool check_inherited)
Y
yangwenjun 已提交
469
{
W
wangjiahui 已提交
470
    if (check_inherited && !ns->separated) {
G
ganlan 已提交
471
        LD_LOGD("is_accessible ns [%{public}s] is not separated, return true.", ns->ns_name);
Y
yangwenjun 已提交
472 473 474 475 476 477 478
        return true;
    }
    if (ns->allowed_libs) {
        char *shortname = strrchr(lib_pathname, '/');
        if (shortname) {
            shortname += 1;
            size_t i = 0;
G
ganlan 已提交
479
            for (; i < ns->allowed_libs->num; i++) {
Y
yangwenjun 已提交
480 481 482 483
                if (strcmp(shortname, ns->allowed_libs->strs[i]) == 0) {
                    break;
                }
            }
W
wangjiahui 已提交
484
            if (i >= ns->allowed_libs->num) {
G
ganlan 已提交
485 486 487
                LD_LOGD("is_accessible ns [%{public}s] lib_pathname [%{public}s] is not in allowed_libs, return false.",
                    ns->ns_name,
                    lib_pathname);
W
wangjiahui 已提交
488 489
                return false;
            }
Y
yangwenjun 已提交
490 491
        }
    }
G
ganlan 已提交
492
    strlist *paths;
Y
yangwenjun 已提交
493
    if (ns->env_paths && (paths = strsplit(ns->env_paths, ":"))) {
G
ganlan 已提交
494
        for (size_t i = 0; i < paths->num; i++) {
Y
yangwenjun 已提交
495 496 497
            size_t len = strlen(paths->strs[i]);
            if (strncmp(lib_pathname, paths->strs[i], len) == 0 &&
                lib_pathname[len] == '/' &&
G
ganlan 已提交
498 499 500 501
                !strchr(lib_pathname + len + 1, '/')) {
                LD_LOGD("is_accessible ns [%{public}s] lib_pathname [%{public}s] in env_paths, return true.",
                    ns->ns_name,
                    lib_pathname);
Y
yangwenjun 已提交
502 503 504 505 506 507 508
                strlist_free(paths);
                return true;
            }
        }
        strlist_free(paths);
    }

W
wangjiahui 已提交
509 510
    if (is_asan) {
        if (check_asan_path(ns, lib_pathname)) {
G
ganlan 已提交
511 512 513
            LD_LOGD("is_accessible ns [%{public}s] lib_pathname [%{public}s] check_asan_path success, return true.",
                ns->ns_name,
                lib_pathname);
W
wangjiahui 已提交
514 515 516 517
            return true;
        }
    }

Y
yangwenjun 已提交
518
    if (ns->lib_paths && (paths = strsplit(ns->lib_paths, ":"))) {
G
ganlan 已提交
519
        for (size_t i = 0; i < paths->num; i++) {
Y
yangwenjun 已提交
520 521 522
            size_t len = strlen(paths->strs[i]);
            if (strncmp(lib_pathname, paths->strs[i], len) == 0 &&
                lib_pathname[len] == '/' &&
G
ganlan 已提交
523
                !strchr(lib_pathname + len + 1, '/')) {
Y
yangwenjun 已提交
524
                strlist_free(paths);
G
ganlan 已提交
525 526 527
                LD_LOGD("is_accessible ns [%{public}s] lib_pathname [%{public}s] in lib_paths, return true.",
                    ns->ns_name,
                    lib_pathname);
Y
yangwenjun 已提交
528
                return true;
G
ganlan 已提交
529
            }
Y
yangwenjun 已提交
530
        }
W
wangjiahui 已提交
531
        strlist_free(paths);
Y
yangwenjun 已提交
532 533
    }

W
wangjiahui 已提交
534
    if (ns->permitted_paths) {
G
ganlan 已提交
535
        for (size_t i = 0; i < ns->permitted_paths->num; i++) {
Y
yangwenjun 已提交
536 537 538
            size_t len = strlen(ns->permitted_paths->strs[i]);
            if (strncmp(lib_pathname, ns->permitted_paths->strs[i], len) == 0 &&
                lib_pathname[len] == '/') {
G
ganlan 已提交
539 540 541
                LD_LOGD("is_accessible ns [%{public}s] lib_pathname [%{public}s] in permitted_paths, return true.",
                    ns->ns_name,
                    lib_pathname);
Y
yangwenjun 已提交
542 543 544 545
                return true;
            }
        }
    }
W
wangjiahui 已提交
546 547
    return false;
}
Y
yangwenjun 已提交
548

W
wangjiahui 已提交
549 550
bool check_asan_path(ns_t *ns, const char *lib_pathname)
{
G
ganlan 已提交
551
    strlist *paths;
W
wangjiahui 已提交
552 553 554 555 556
    if (ns->asan_lib_paths && (paths = strsplit(ns->asan_lib_paths, ":"))) {
        for (size_t i = 0; i < paths->num; i++) {
            size_t len = strlen(paths->strs[i]);
            if (strncmp(lib_pathname, paths->strs[i], len) == 0 &&
                lib_pathname[len] == '/' &&
G
ganlan 已提交
557
                !strchr(lib_pathname + len + 1, '/')) {
W
wangjiahui 已提交
558
                strlist_free(paths);
G
ganlan 已提交
559 560 561
                LD_LOGD("check_asan_path ns [%{public}s] lib_pathname [%{public}s] in asan_lib_paths, return true.",
                    ns->ns_name,
                    lib_pathname);
W
wangjiahui 已提交
562
                return true;
G
ganlan 已提交
563
            }
W
wangjiahui 已提交
564 565 566 567 568 569 570 571
        }
        strlist_free(paths);
    }
    if (ns->asan_permitted_paths) {
        for (size_t i = 0; i < ns->asan_permitted_paths->num; i++) {
            size_t len = strlen(ns->asan_permitted_paths->strs[i]);
            if (strncmp(lib_pathname, ns->asan_permitted_paths->strs[i], len) == 0 &&
                lib_pathname[len] == '/') {
G
ganlan 已提交
572 573 574 575
                LD_LOGD(
                    "check_asan_path ns [%{public}s] lib_pathname [%{public}s] in asan_permitted_paths, return true.",
                    ns->ns_name,
                    lib_pathname);
W
wangjiahui 已提交
576 577 578 579
                return true;
            }
        }
    }
G
ganlan 已提交
580 581
    LD_LOGD(
        "check_asan_path ns [%{public}s] lib_pathname [%{public}s] failed, return false.", ns->ns_name, lib_pathname);
Y
yangwenjun 已提交
582 583 584 585 586 587
    return false;
}

bool is_sharable(ns_inherit *inherit, const char *lib_name)
{
    if (inherit && lib_name && inherit->shared_libs) {
G
ganlan 已提交
588
        for (size_t i = 0; i < inherit->shared_libs->num; i++) {
Y
yinchuang 已提交
589 590
            if (strcmp(inherit->shared_libs->strs[i], lib_name) == 0 ||
                strcmp(inherit->shared_libs->strs[i], ALLOW_ALL_SHARED_LIBS) == 0) {
G
ganlan 已提交
591 592 593
                LD_LOGD("is_sharable inherit [%{public}s] lib_name [%{public}s] found, return true.",
                    inherit->inherited_ns->ns_name,
                    lib_name);
Y
yangwenjun 已提交
594 595 596
                return true;
            }
        }
G
ganlan 已提交
597 598 599
        LD_LOGD("is_sharable inherit [%{public}s] lib_name [%{public}s] not found, return false.",
            inherit->inherited_ns->ns_name,
            lib_name);
Y
yangwenjun 已提交
600 601
        return false;
    }
G
ganlan 已提交
602
    LD_LOGD("is_sharable shared_libs not config, return true.");
Y
yangwenjun 已提交
603 604
    return true;
}