2011-03-23 4 views
4

인터뷰 관련 질문입니다. 인터뷰 관련 서적과 나중에 인터뷰에서 나왔습니다.특정 주소에 값을 지정하십시오.

문제는 주소 (말은 0x12345678)에 값을 할당하는 방법

(예를 들어 0)입니다.

(인터뷰 후 오랜 시간 후)이 질문에 대한 나의 최선의 시도는 주소가 포인터에 저장 될 수있는 숫자입니다

이며, 우리는 주소에 값을 할당 할 수 있습니다 포인터에 의해 값이 같은 주소이다 : 그러나

  int* p = 0x12345678; 
     *p = 0; 

가 그 메모리 관리를 갖는 시스템에서 불가능하다 progr 때문에 오전에는 특정 주소에 대한 특권이 없습니다.

필자 자신의 경험에 따르면, 이러한 종류의 작업이 유효한 유일한 시간은 운영 체제가없는 8086 칩의 실험이었고 그 시간에 사용한 언어는 어셈블리였습니다.

제 대답을 개선하고 개선하는 데 도움을주십시오. 감사.

답변

-1

아마도 메모리 관리가있는 시스템에서는 불가능하기 때문에 대답이 없을 수도 있습니다.

메모리 관리가없는 시스템에서는 어셈블리 코드를 직접 사용하려고합니다.

포인터, 그것은 잘못거야 현재 프로세스에 대한 접근 공간이 외부 경우는, 가장 확실히

+0

이들은 모두 내 질문에 실제로 적용됩니다. 나는 (열린) 질문에 대해 내가 무엇을 이야기해야 하는지를 알고 싶다. –

0

그런 일을하는 데 도움이, 심지어 메모리 관리와 시스템에서 가능 희망하지만, 그런 일을 해야하는거야. 그렇지 않은 경우 값이 설정되고 이동합니다. 그것 이외에, 당신은 나에게 잘하는 것 같습니다.

+0

_possible_은 컴파일 할 수 있지만 런타임 예외로 실행된다는 의미입니까? –

3

코드가 정확하지만 OS가 0x12345678을 읽기 전용으로 정의하면 런타임시 오류가 발생할 수 있습니다.

"일반"OS는 그렇게하지만 "더 가벼운"OS는 그렇지 않습니다.

커널 - 해킹 프로그램을 작성하고 싶습니다.


1)이 모듈을 구축 (예 : 당신이 살펴보고 좋아하면

나는 리눅스를 위해 그것을 해결했다.코) :

#include <linux/module.h> 
#include <linux/fs.h>  /* for file_operations */ 
#include <linux/uaccess.h> /* copy_from & copy_to */ 

char* g_value=0; 
size_t size =0; 

int driver_open(struct inode *inode, struct file *filp) 
{ 
    printk("open driver"); 
    return 0; 
} 

int driver_write(struct file*,   /*ignored*/ 
       const char __user *umem,/*source in user-space's address*/ 
       size_t size,   /*max size to be writen*/ 
       loff_t*)    /*offset - ignored*/ 
{ 
    unsigned long ret = 0; 

    g_value = (char*) kmalloc(size, GFP_KERNEL); 

    if (!g_value) 
    { 
     printk("ERROR:allocation failure\n"); 
     return -ENOMEM; 
    } 

    ret = copy_from_user(g_value, /*destination*/ 
         umem,  /*source*/ 
         size);  /*size*/ 

    if (ret<0) 
    { 
     printk("ERROR:copy failure\n"); 
     return -EACCES; 
    } 

    return g_size = size;; 
} 

int driver_read(struct file*,  /*ignored*/ 
       char __user *umem, /*destination in user-space's address*/ 
       size_t size,  /*max size to be read*/ 
       loff_t*)   /*offset - ignored*/ 
{ 

    /* don't use copy_to_user(umem, &value, size)!! 
     we want to do exectly what it is made to protect from */ 

    int i = ((g_size>size)?size:g_size)-1; /*MIN(g_size,size)-1*/ 
    for (; i>=0; --i) 
    { 
     umem[i]=g_value[i]; /*can be done more effectively, thats not the point*/ 
    } 

    return size; 
} 

int driver_close(struct inode *inode, struct file *filp) 
{ 
    if (g_value) 
     free(g_value); 
    g_value = 0; 
    printk("close driver"); 
    return 0; 
} 

/***interface***/ 

struct file_operations driver_ops = { 
    open: driver_open, 
    write: driver_write, 
    read: driver_read, 
    release: driver_close 
}; 

/***implementation***/ 

static int g_driver_fd = 0; 

static void driver_cleanup(void) 
{ 
    printk("ERROR:driver exit\n"); 
    unregister_chrdev(g_driver_fd, "driver"); 
} 

static int driver_init(void) 
{ 

    printk("driver init\n"); 
    g_driver_fd = register_chrdev(0,"ROM-bypass", &driver_ops); 
    if (g_driver_fd<0) 
    { 
     printk("ERROR:failed to register char driver\n"); 
     return -1; 
    } 
    return 0; 
} 

module_init(driver_init); 
module_exit(driver_cleanup); 

/***documentation***/ 

MODULE_DESCRIPTION("write on OS's \"read only\" segment"); 
MODULE_AUTHOR("Elkana Bronstein"); 
MODULE_LICENSE("GPL"); 

2) 커널 모듈에 추가 :

$cat /proc/devices 

4) 노드와 관련된합니다

$insmod example.ko 

3) 목록에서 모듈의 '주요'발견 장치 :

$mknod /dev/rom_bypass c <major> <minor> 

'c'는 문자 d evice과 '미성년자') 255

5 중 하나가 될 수있는 파일로 코드에서 장치를 사용할 수 있습니다 : (거의) 불가능 메모리 위치를 작성하는 데 사용할 수있는 알 수있다

int main() 
{ 

    int fd; 
    int value = 0; 

    fd = open("/dev/rom_bypass",O_RDWR);  
    if (fd<0) 
    { 
     fprintf(stderr,"open failed"); 
     return -1; 
    } 

    /*write the desirable value into the device's buffer*/ 
    write(fd,&value,sizeof(value)); 
    /*read the device's buffer into the desirable object - without checking*/ 
    read(fd,0x12345678,sizeof(value)); 

    close(fd); 
} 
1

에.

OS에서 malloc() 함수를 사용하여 사용 가능한 주소를 제공하고 나중에 free()를 사용하여 해당 위치를 확보하도록 요청할 수 있습니다.

또 다른 방법은 스택 메모리를 사용하는 것입니다. 그냥 변수를 정의 int * p = 0; & p가이 위치의 주소를 알려줍니다.

사용할 수없는 위치에 값을 할당하려고하면 세그먼테이션 오류 오류가 발생할 수 있습니다.

희망이 도움이됩니다.

+0

오, 방금 데이트를 보았습니다. 2 년 늦은 것 같아요. P – user2438252

관련 문제