CS241 Overview Operating System Design University of Illinois at Urbana-ChampaignOverviewOperating System Structure (1)Operating System Structure (2)Operating System Structure (3)Operating System Structure (4)Operating System Structure (5)Metric UnitsPrograms R:p14-38Buffer OverflowSlide 11Password check susceptible to buffer overflowStack Layout for checkpassProgram LayoutR: Appendix ALibrary Function CallsError reportingMany Library Calls abort if process is interrupted by signalHandling ErrorsArgument ArraysArgument ArraysCreating Argument ArraysHow to write makeargvSlide 24Implementation of makeargv R:p37Making Functions SafeSlide 27Storage classes: static and automaticLinkage classesVariablesFunctionsSummary01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved1CS241CS241OverviewOverview Operating System Design Operating System DesignUniversity of Illinois at Urbana-ChampaignUniversity of Illinois at Urbana-Champaign Professor: Roy Campbell01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved2Overview•Operating System Design•Programs R:14-38•Buffer overflow•Arguments•Libraries01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved3Operating System Structure (1)Simple structuring model for a monolithic system01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved4Operating System Structure (2)Structure of the THE operating system01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved5Operating System Structure (3)Structure of VM/370 with CMS01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved6Operating System Structure (4)The client-server model01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved7Operating System Structure (5)The client-server model in a distributed system01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved8Metric UnitsThe metric prefixes01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved9Programs R:p14-38 The importance of writing correct programsBuffer overflowProgram LayoutStatic DataLibrary Function CallsFunction Return Values and ErrorsArgument Arrays01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved10Buffer Overflowchar buf[80];printf(“Enter your first name”);scanf(“%s,buf);Also note, strings end in a \0.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved11#include <stdio.h> #include <string.h> int checkpass(void){ int x; char a[9]; x = 0; fprintf(stderr,"a at %p and\nx at %p\n", (void *)a, (void *)&x); printf("Enter a short word: "); scanf("%s", a); if (strcmp(a, "mypass") == 0) x = 1; return x; }01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved12Password check susceptible to buffer overflow#include <stdio.h> int checkpass(void); int main(void) { int x; x = checkpass(); fprintf(stderr, "x = %d\n", x); if (x) fprintf(stderr, "Password is correct!\n"); else fprintf(stderr, "Password is not correct!\n"); return 0; }01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved13Stack Layout for checkpassaunusedxSaved frame pointerReturn addresstopbase10001009101210161020102401/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved14Program Layout01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved15R: Appendix AMan PagesCompilationHeader FilesLinking and librariesMacros and conditional compilationMakefilesDebugging Aids – lint, debugger, trussIdentifiers, Storage Classes and Linkage Classes01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved16Library Function CallsUNIX – returns 0 successful, -1 unsuccessful, sets errnoPOSIX – all new functions return error code and do not use errnoGood coders handle ALL errors, not just mandatory (in the standard) ones.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved17Error reporting#include <stdio.h>Void perror(const char * s);--------------------------#include <string.h>Char *strerror(int errnum);01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved18Many Library Calls abort if process is interrupted by signalint error;int fildes;while (((error = close(fildes))== -1) && (errno == EINTR));if (error == -1) perror(“Failed to close the file”);01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved19Handling ErrorsAlways handle all errorsEither:Print error message and exit program (only in main)Return -1 or NULL and set an error indicator such as errnoReturn and error codeAll functions should report errors to calling programUse conditional compilation to enclose debugging print statements01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved20Argument Arraysint main(int argc, char * argv[])Argc is number of arguments, argv is an array of pointers to the tokens01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved21Argument Arrays An argument array is an array of pointers terminated by a NULL pointer.Each element of the array is of type char * and represents a string.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved22Creating Argument ArraysSometimes it is necessary to create a structure like this yourself from a string.The shell must do this when you execute a command. argv[] is an array of pointers to charsIn C, this is the same as a pointer to a pointer to a char. One way to write a function to do this is:char **makeargv(char *s)If you want to return the number of tokens, you can pass a pointer to the arg array as in:int makeargv(char *s, char ***argvp)The version we will use has an additional parameter that specifies a string of delimiters:int makeargv(const char *s, const char *delimiters, char ***argvp)The const for the first two parameters indicates that the strings should not be modified by the function.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved23How to write makeargv Can use strtok: #include <string.h>char *strtok(char *restrict s1, const char *restrict s2); s1 is the string to parses2 is a string of delimitersreturns a pointer to the next token or NULL if none left.First call: s1 points to the string to parseAdditional calls: s1 is NULL.Note: the string s1 is modified. We do not want the string passed to makeargv to be modified so we allocate space for another copy of the string. Make a pass with strtok to count the tokens.Use the
View Full Document