WEB开发网
开发学院操作系统Linux/Unix AIX 6 和 POWER 6 中的内存保护存储键(storage k... 阅读

AIX 6 和 POWER 6 中的内存保护存储键(storage keys)

 2008-09-06 08:19:31 来源:WEB开发网   
核心提示: 现在就可以对受保护的内存进行读写了,如果您需要运行不向其授予写访问权限的代码:uketset_activate(reader, UKA_REPLACE_KEYS);此时调用 dodgy 函数和写访问权限,AIX 6 和 POWER 6 中的内存保护存储键(storage keys)(3),

现在就可以对受保护的内存进行读写了。

如果您需要运行不向其授予写访问权限的代码:uketset_activate(reader, UKA_REPLACE_KEYS);
此时调用 dodgy 函数和写访问权限,以生成 SIGSEGV 信号
uketset_activate(writer, UKA_REPLACE_KEYS);

如果您需要运行甚至不向其授予读访问权限的代码:uketset_activate(none, UKA_REPLACE_KEYS);
此时调用 dodgy 函数和写访问权限,以生成 SIGSEGV 信号
uketset_activate(writer, UKA_REPLACE_KEYS);

示例代码

以下是我使用存储键的简单示例:#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/vminfo.h>
#include <sys/ukeys.h>
#define PERROR(argument) { perror(argument); exit(1); }
#define ROUND_UP(size,psize) ((size)+(psize)-1 & ~((psize)-1))
struct important_data {
    int   some_data[1379];
} *p1 ;             /* pointer to protected data structure */
ukeyset_t reader;
ukeyset_t writer;
int   untrusted_reader(void)
{
    return 1+ p1->some_data[42]; /* We can read protected data */
}
int   untrusted_writer(void)
{
    p1->some_data[42] = (p1->some_data[42] +1) * 2;
    return p1->some_data[42];
}
/* Signal handler to catch the deliberate protection violation SIGSEGV */
void  sighandler(int signo)
{
    printf("Sighandler() signo %d (SIGSEGV=11)n", signo);
    printf("Warning No keys active herenExit Signal Handlern");
}
main()
{
    int   nkeys;      /* number of memory keys */
    int   pagesize = 4096; /* hardware data page size */
    int   pagesize_padded; /* page padded size of protected data */
    struct vm_page_info page_info; /* used to find the page size */
    /* Phase 1: Attempt to become user key aware */
    if ((nkeys = ukey_enable()) == -1) PERROR("ukey_enable");
    printf("There are %d memory keys availabledn",nkeys);
    /* Determine the data region page size */
    page_info.addr = (long)&p1; /* address in data region */
    if (vmgetinfo(&page_info, VM_PAGE_INFO, sizeof(struct vm_page_info )))
        PERROR("vmgetinfo")
    else
        pagesize = page_info.pagesize; /* pick up actual page size */
    printf("Page size is %d, data size is %dn", pagesize,sizeof(struct
                                 important_data));
    /* Allocate page aligned, page padded memory buffer */
    pagesize_padded = ROUND_UP(sizeof(struct important_data ), pagesize);
    if (posix_memalign((void **) & p1, pagesize, pagesize_padded))
        PERROR("posix_memalign");
    printf("Allocated %d bytes at location 0x%pn", pagesize_padded, p1);
    /* Phase 2: Initialize and protect the private data. */
    /* Note that the pointer to the private data is in public storage. */
    /* We only protect the data itself. */
    p1->some_data[42] = 0; /* normal access = unprotected so far */
    /* Restrict access to the private data page(s) using private key */
    if (ukey_protect(p1, pagesize_padded, UKEY_PRIVATE1))
        PERROR("ukey_protect");
    /* Phase 3: Gain access to the protected data */
    /*  Construct keysets for to access the protected structure. */
    /*  Note that these keysets will be in public storage. */
    if (ukeyset_init(&reader, 0))
        PERROR("ukeyset_init");
    if (ukeyset_add_key(&reader, UKEY_PRIVATE1, UK_READ)) /* READ */
        PERROR("ukeyset_add_key 1W");
    writer=reader;
    if (ukeyset_add_key(&writer, UKEY_PRIVATE1, UK_RW)) /* R/W */
        PERROR("ukeyset_add_key 1R");
    /* Allow our general code to reference the private data R/W. */
    if (ukeyset_activate(writer, UKA_REPLACE_KEYS) == UKSET_INVALID)
        PERROR("ukeyset_activate");
    /* Program's main processing loop. */
    printf("Current key is RW - read should workn");
    untrusted_reader();
    printf("Current key is RW - write should workn");
    untrusted_writer();
    (void)ukeyset_activate(reader, UKA_REPLACE_KEYS);
    printf("Current key is read-only - read should workn");
    untrusted_reader();
    printf("Current key is read-only - write should failn");
    untrusted_writer();
    printf("Make protected memory public before we endn");
    ukey_protect(p1, pagesize_padded, UKEY_PUBLIC);
    free(p1);
}

上一页  1 2 3 4  下一页

Tags:AIX POWER 内存

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接