Question

In: Computer Science

How many race conditions does attackers have to win in the following program? int main() {...

How many race conditions does attackers have to win in the following program?

int main() {

struct stat stat1, stat2;

int fd1, fd2;

if (access("/tmp/XYZ", O_RDWR)) {

fprintf(stderr, "Permission denied\n");

return -1;

}

else fd1 = open("/tmp/XYZ", O_RDWR);

if (access("/tmp/XYZ", O_RDWR)) { '

fprintf(stderr, "Permission denied\n");

return -1;

}

else fd2 = open("/tmp/XYZ", O_RDWR);

// Check whether fd1 and fd2 have the same inode.

fstat(fd1, &stat1);

fstat(fd2, &stat2);

if(stat1.st_ino == stat2.st_ino) {

write_to_file(fd1);

}

else {

fprintf(stderr, "Race condition detected\n");

return -1;

}

return 0;

}

Solutions

Expert Solution

The accessed file in the code snippet "/tmp/XYZ" . In the given code snippet, three race conditions are possible. Race condition is a situation when two or more operations is trying to be done on a critical section (or on a specific file) at the same time. In that case, depending on the sequence of the work flow, results can vary.

Analysis of the code:

The first race can occur between the first access of the file and fd1 (open the file). Here, inside the first if condition, the file will be accessed and fd1 will open the same file by using pointers. Thus, in a situation, both the lines can lead to a race condition since no semaphore is used or checking of the availability of resource is done here.

The second race can occur between the fd1 (open the file) and the second access under the second if condition. Here fd1 will open the file using pointer and the condition under second if statement will try to access the file. Both of them can lead to a race condition since no semaphore is used or checking of the availability of resource is done.

The third race can occur between the second access and fd2 (open the file). Here, inside the second if condition, the file will be accessed and fd2 will open the same file by using pointers. Since no semaphore is used or availability of the resource is checked, it can lead to a race condition.

Conclusion: Thus total three race conditions have to win by attackers.


Related Solutions

How many new processes will be created by each of the following program segment: a.         int...
How many new processes will be created by each of the following program segment: a.         int i;             for (i = 0; i < 1000; i++)                         if (fork()==0) break; b.         int i;             for (i = 0; i < 1000; i++)                         if (fork()!=0) break; c.         int i, pid;             for (i = 0; i < 1000; i++)                         pid = fork();
Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;...
Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;       char B=10; cout<<”Enter Value of A”; cin<<A; show(A) show(50)          }       cin.get(); } int show(int x); { cout<<”Value is =” <<X cout<<endl; }
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000];...
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000]; for(i=0;i<1000;i++) a[i]=1;    for(i=2;i<1000;i++) { if(a[i]==1){ for(j=i+1;j<1000;j++) {if(j%(i)==0) a[j]=0; } } } printf("print numbers are:\n"); for(i=2;i<=1000;i++) if(a[i]==1) printf("%d, ",i); }
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number;...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number; Scanf (“%d”, & number); if (number % 2 ==0) { printf (“Even\n”); } else { printf(“Odd\n”); } Return 0; }
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main(){ int numItms, j,...
Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main(){ int numItms, j, data, sum; scanf("%d",&numItms); sum = 0; for(j = 1; j <= numItms; j++){ scanf("%d",&data); sum += data; } printf("Sum:%d\n",sum); return 0; } SAMPLE INPUT: 48-376 SAMPLE OUTPUT: Sum: 18
How many electrons does a neutral potassium atom have? How many protons? How many electrons does...
How many electrons does a neutral potassium atom have? How many protons? How many electrons does a neutral fluorine atom have? What happens in the molecule of potassium fluoride?
please explain how does the following C code work. a) int function1(int num1, int num2) {...
please explain how does the following C code work. a) int function1(int num1, int num2) { int num = num1 ^ num2; int result = 0; while(num > 0) { result += (num & 1); num >>= 1; } return result; } b) int function2(unsigned int num) { return num & ~(num - 1); } c) int f1(unsigned int x) { int count = 0; while(x) { count++; x = x&(x-1); } return count; } d) double ldexp(double x, int...
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum;...
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum; scanf("%d", &numitms); sum=0; for (j=1;j<=numitms;j++) { scanf("%d", &data); sum+=data; } printf("sum: %d\n",sum); return0; } I got an answer with an error. Please debug the answer or have a new correct answer (don't copy and paste others' answer) main: SUBSP 2,i DECI numItms,i DECI j,j DECI data,d DECI sum,s LDWA numItms,i sum: .EQUATE 0 LDWA 1,i STWA j,j for: CPWA numItms, j BRGE endFor STRO...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT