Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Funcionamiento codigo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ UPROGS=\
$U/_logstress\
$U/_forphan\
$U/_dorphan\
$U/_mrdprotect\
$U/_mrtest\

fs.img: mkfs/mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS)
Expand Down
48 changes: 0 additions & 48 deletions README

This file was deleted.

36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# INFORME Tarea 3: Proteccion de Lectura en XV6

**Integrante:** Diego Gajardo
**Usuario Github:** Wajax8
**Rama:** Diego_Gajardo_T3

## 1. Pasos realizados:
1. Se definen las nuevas syscalls 'mrdprotect' y 'munrdprotect' y declaramos sus respectivos numeros en 'kernel/syscall.h'
2. se implementan tambien en 'sysproc.c' llamando asi a las funciones de kernel necesarias
3. En 'kernel/systemcall.c' se agrega las nuevas funciones al listado de systemcalls, para que asi el kernel las reconozca y se ejecuten cuando se llamen
4. se agrego las funciones a 'user/usys.pl' para que los programas puedan llamar a las syscalls
5. luego se añaden a 'user/user.h' para que cualquier programa puedan usarlas normalmente
6. luego en 'vm.c'se agrego la parte la parte encargada de el permiso de lectura, protegiendo o desprotegiendola
7. Se ajusta 'kernel/trap.c' para que se detecte cuando un programa intenta leer una pagina la cual se encuentre protegida
8. Luego de todos los ajustes necesarios se crea el archivo 'user/mrdprotect.c' el cual es el que contiene los wrappers
9. se añade en los UPFROGS los cuales se encuentran en 'Makefile'
10. Luego se crea otro archivo el cual es 'user/mrtest' el cual se encarga del test y de la restauracion

## 2. Comandos para compilar, ejecutar y testear
Compilacion: `make clean` = limpia los archivos
`make` = los compila
Inicio XV6: `make qemu` = procede a ejecutar el sistema operativo
Cierre XV6: `Control + A` luego `X`

ejecutar tests: `mrdprotect` se prueba la proteccion y `mrtest` prueba la restauracion

## 3. Promp de la Ia usada como ayuda
https://chatgpt.com/share/693787fb-fb94-8006-87a3-43feb90ae45c

## 4. Problemas encontrados
- El principal problema fue debido a los commits debido a que despues de commitear no se me guardo realmente pero me aparecia en el codespace como si estubiera guardado y al intentar hacer que los 2 esten vinculados se borro mi progreso. La solucion si bien fue volver a hacerlo revisando que cosas habia cambiado ya que me acordaba de que archivos habia modificado, hubiera sido mejor realizar copias de seguridad periodicamente para prevenir esto

- el segundo problema fue entender que codigos y archivos se tenian que modificar realmente, ya que al usar una Ia como ayuda, este no entiende muy bien que archivos se deben modificar realmente y sobretodo derrepente cambia el nombre de las variables o funciones. La solocion a esto seria utilizar la Ia con precaucion y revisar bien los datos que esta nos brinda

## 5. Imagen del codigo funcionando
![Funcionamiento del codigo](Funcionamiento%20codigo.png)
10 changes: 10 additions & 0 deletions d
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
* d26c508 (HEAD -> Diego_Gajardo_T3, origin/Diego_Gajardo_T3) Update README con los pasos realizados
* 257bfd2 Create mrdprotect.c
* e90b257 (origin/riscv, origin/HEAD, origin/Diego_Gajardo_T0, riscv) explicitly specify -march=rv64gc
* df440e2 slightly clearer comments, for next year
* c71a6c4 ensure sbrk cannot exceed TRAPFRAME
* 8402fc9 add a test for sbrk approaching MAXVA
* 4689d66 fix piperead() with lazy sbrk
* ffdff7c Replace stale comment with old comment, after lazy-page allocation changes
* c14b639 pass -Wno-unknown-attributes for mkfs too
* 9dbd536 Update comment
2 changes: 2 additions & 0 deletions kernel/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ int copyin(pagetable_t, char *, uint64, uint64);
int copyinstr(pagetable_t, char *, uint64, uint64);
int ismapped(pagetable_t, uint64);
uint64 vmfault(pagetable_t, uint64, int);
int mrdprotect(void *addr, int len);
int munrdprotect(void *addr, int len);

// plic.c
void plicinit(void);
Expand Down
5 changes: 5 additions & 0 deletions kernel/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ extern uint64 sys_unlink(void);
extern uint64 sys_link(void);
extern uint64 sys_mkdir(void);
extern uint64 sys_close(void);
extern uint64 sys_mrdprotect(void);
extern uint64 sys_munrdprotect(void);

// An array mapping syscall numbers from syscall.h
// to the function that handles the system call.
Expand All @@ -126,6 +128,9 @@ static uint64 (*syscalls[])(void) = {
[SYS_link] sys_link,
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
[SYS_mrdprotect] sys_mrdprotect,
[SYS_munrdprotect] sys_munrdprotect,

};

void
Expand Down
2 changes: 2 additions & 0 deletions kernel/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
#define SYS_link 19
#define SYS_mkdir 20
#define SYS_close 21
#define SYS_mrdprotect 22
#define SYS_munrdprotect 23
25 changes: 25 additions & 0 deletions kernel/sysproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,28 @@ sys_uptime(void)
release(&tickslock);
return xticks;
}

uint64
sys_mrdprotect(void)
{
uint64 addr;
int len;

argaddr(0, &addr);
argint(1, &len);

return mrdprotect((void*)addr, len);
}

uint64
sys_munrdprotect(void)
{
uint64 addr;
int len;

argaddr(0, &addr);
argint(1, &len);

return munrdprotect((void*)addr, len);
}

38 changes: 38 additions & 0 deletions kernel/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,41 @@ ismapped(pagetable_t pagetable, uint64 va)
}
return 0;
}

int
mrdprotect(void *addr, int len)
{
uint64 a = (uint64) addr;
struct proc *p = myproc();
pte_t *pte;

for (int off = 0; off < len; off += PGSIZE) {
pte = walk(p->pagetable, a + off, 0);
if (pte == 0)
return -1;

*pte &= ~PTE_R; // quitar lectura
}

sfence_vma();
return 0;
}

int
munrdprotect(void *addr, int len)
{
uint64 a = (uint64) addr;
struct proc *p = myproc();
pte_t *pte;

for (int off = 0; off < len; off += PGSIZE) {
pte = walk(p->pagetable, a + off, 0);
if (pte == 0)
return -1;

*pte |= PTE_R; // restaurar lectura
}

sfence_vma();
return 0;
}
32 changes: 32 additions & 0 deletions user/mrdprotect.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "kernel/types.h"
#include "user/user.h"

int
main()
{
char *addr = sbrk(4096); // Reservar una página
addr[0] = 'X';

printf("Antes de proteger: %c\n", addr[0]);

if (mrdprotect(addr, 1) < 0) {
printf("mrdprotect falló\n");
exit(1);
}

printf("Protegido contra lectura. Voy a intentar leer...\n");

// Esto DEBE causar un fallo (page fault)
char c = addr[0];

// Si llega aquí, es un error
printf("Leí: %c (NO debería pasar)\n", c);

if (munrdprotect(addr, 1) < 0) {
printf("munrdprotect falló\n");
exit(1);
}

printf("Protección revertida.\n");
exit(0);
}
30 changes: 30 additions & 0 deletions user/mrtest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "kernel/types.h"
#include "user/user.h"

int main() {
char *addr = sbrk(4096);
addr[0] = 'X';

printf("Antes: %c\n", addr[0]);

if (mrdprotect(addr, 1) < 0) {
printf("mrdprotect falló\n");
exit(1);
}

printf("Intentando leer mientras está protegido...\n");

// NO vamos a leer -> evitamos el page fault

printf("Ahora desprotegiendo...\n");

if (munrdprotect(addr, 1) < 0) {
printf("munrdprotect falló\n");
exit(1);
}

// Ahora sí debe poder leerse
printf("Después de desproteger: %c\n", addr[0]);

exit(0);
}
4 changes: 3 additions & 1 deletion user/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ int getpid(void);
char* sys_sbrk(int,int);
int pause(int);
int uptime(void);
int mrdprotect(void *addr, int len);
int munrdprotect(void *addr, int len);

// ulib.c
int stat(const char*, struct stat*);
Expand All @@ -46,4 +48,4 @@ void printf(const char*, ...) __attribute__ ((format (printf, 1, 2)));

// umalloc.c
void* malloc(uint);
void free(void*);
void free(void*);
2 changes: 2 additions & 0 deletions user/usys.pl
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ sub entry {
entry("sbrk");
entry("pause");
entry("uptime");
entry("mrdprotect");
entry("munrdprotect");