---
# System prepended metadata

title: so lista 6

---

# so lista 6
https://hackmd.io/@whiskeyo/HyLSEL1Kv#Zadanie-22

## Zadanie 1(done)
![](https://i.imgur.com/UYwoTMc.png)

ruid = 1000
euid = 0
suid = 0

------------
a) setuid(2000)
![](https://i.imgur.com/owg0249.png)

ruid = 2000
euid = 2000
suid = 2000
bo euid =  0
b) setreuid(-1,2000)
![](https://i.imgur.com/9x4O6T6.png)
ruid = 1000
euid = 2000
suid = 2000

c) seteuid(2000)
![](https://i.imgur.com/U5exQzM.png)
ruid = 1000
euid = 2000
suid = 0

d) setresuid(-1,2000,3000)
![](https://i.imgur.com/XT1NPpi.png)
ruid = 1000
euid = 2000
suid = 3000

Nie jest to privledged user bo effective nie jest 0


## Zadanie 2 (done)

![](https://i.imgur.com/M3kUvxm.png)
rwx:
r - przeglądaj pliki w katalogu (jego zawartosc)
w - twórz nową zawartość np. nowy plik 
x - 
![](https://i.imgur.com/zsAvmJi.png)
![](https://i.imgur.com/7McyYUv.png)

----------------------------

set-gid -

![](https://i.imgur.com/0Y5tl3c.png)

sticky -

![](https://i.imgur.com/kNnXtGp.png)

-------------------------------
![](https://i.imgur.com/k41f0Fy.png)

```c=
bool my_access(struct stat *sb, int mode) {
  
    if (geteuid() == 0)
    {
        return true;
    }
    
    if(geteuid() == getuid())
    {
        int sb_st_mode = sb -> st_mode;
        sb_r = (sb_st_mode & S_IRUSR);
        sb_w = (sb_st_mode & S_IWUSR);
        sb_x = (sb_st_mode & S_IXUSR);

        mode_r = (mode & R_OK);
        mode_w =  (mode & W_OK);  
        mode_x = (mode & X_OK);

        if((sb_r == mode_r) && (sb_w == mode_w) && (sb_x == mode_x) || mamy permissiony, ale nie musimy ich mieć)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
    
    sb_groupid = sb -> st_gid;
    group = (gid_t *)malloc(NGROUPS_MAX  * sizeof(gid_t));
    int groups = getgroups(NGROUPS_MAX , group);

    for (int i = 0; i < groups; i++)
    {
        if (sb_groupid == group[i])
        {
            int sb_st_mode = sb -> st_mode;
            sb_gr = (sb_st_mode & S_IRGRP);
            sb_gw = (sb_st_mode & S_IWGRP);
            sb_gx = (sb_st_mode & S_IXGRP);

            mode_r = (mode & R_OK);
            mode_w =  (mode & W_OK);  
            mode_x = (mode & X_OK);
            if((sb_gr == mode_r) && (sb_gw == mode_w) && (sb_gx == mode_x) || mamy permissiony, ale nie musimy ich mieć)
            {
                return true;
            }
            else
            {
                return false;
            }
            }
          
    }
    
        sb_st_mode = sb -> st_mode;
        sb_or = (sb_st_mode & S_IROTH);
        sb_ow = (sb_st_mode & S_IWOTH);
        sb_ox = (sb_st_mode & S_IXOTH);

        mode_r = (mode & R_OK);
        mode_w =  (mode & W_OK);  
        mode_x = (mode & X_OK);

        if((sb_or == mode_r) && (sb_ow == mode_w) && (sb_ox == mode_x) || mamy permissiony, ale nie musimy ich mieć)
        {
            return true;
        }
        else
        {
            return false;
        }

    
    
    return false; 
}

```

## Zadanie 3 (to do)
![](https://i.imgur.com/xBq319v.png)

## Zadanie 4 (done)

![](https://i.imgur.com/lR0W9N7.png)
czemu least prilidged ?
-żeby nie rozprzestrzeniac malware,
-zeby byc odpornym na ataki 

Jak odbierac?
![](https://i.imgur.com/NKeVrNd.png)
Zasady:
-usuń uprawnienia jesli nigdy juz nie bedą potrzrebne 

![](https://i.imgur.com/tfv4jGG.png)

![](https://i.imgur.com/zBGlCPT.png)

![](https://i.imgur.com/3N8rIsH.png)

Capabilities:
![](https://i.imgur.com/OJWfJ1P.png)

![](https://i.imgur.com/akDZm9t.png)

![](https://i.imgur.com/5Gtuyoc.png)

![](https://i.imgur.com/Zx6xbIx.png)

Aby proces użytkownika wysłał sygnał do innego procesu, powinien być uprzywilejowany (mieć zdolność CAP_KILL) lub ruid/euid procesu użytkownika musi być równe ruid lub suid procesu docelowego. W przypadku SIGCONT wystarczająca jest przynależność do jednej sesji (procesów wysyłających i odbierających).

## Zadanie 5 ()
![](https://i.imgur.com/5MoPszn.png)

Jakie zadanie pełni procedure exit(3)
![](https://i.imgur.com/vxkw3O7.png)

Problem z buforowaniem plikow

![](https://i.imgur.com/DNmf8OL.png)
![](https://i.imgur.com/SgKSsQ5.png)


Fork:
jak robimy fork bez fflush() to buffer sie uplikuje do dziecka i dziecko wypisuje to co parent 
np.https://stackoverflow.com/questions/11346131/buffering-mechanism-when-fork-is-used-in-c


execve():
to samo co w forku - musimy wyczyścic buffer pzred kolejnym execve  przyklad: https://stackoverflow.com/questions/59939773/how-to-clear-a-bytes-buffer-that-is-set-as-stdout-in-exec-command-in-golang-b-r


_exit():
The _Exit() [CX] and _exit() functions shall not call functions registered with atexit() nor any registered signal handlers. [CX] Open streams shall not be flushed. Whether open streams are closed (without flushing) is implementation-defined. Finally, the calling process shall be terminated with the consequences described below.

czyli tez nie flushuje buffera 




-----------------------


strategie buforowania:

(a) plikiem terminala - Characters written to a line buffered stream are transmitted to the file in blocks when a newline character is encountered.


(b) plikiem zwykłym - Characters written to or read from a fully buffered stream are transmitted to or from the file in blocks of arbitrary size.

(c) standardowym wyjściem błędów «stderr» - Characters written to or read from an unbuffered stream are transmitted individually to or from the file as soon as possible.


------

co to znaczy wielobiezne?
![](https://i.imgur.com/ZGJJrnq.png)
https://www.gnu.org/software/libc/manual/html_node/Nonreentrancy.html

https://man7.org/linux/man-pages/man7/signal.7.html