============================================= ======== CS 33 Lab 3: Stack Smashing ======== ======== Tips on getting started ======== ============================================= This is a quick tutorial on how to get started on lab3. Hopefully this is helpful for those that are not very familiar with the unix command line. For reference, the lab spec is here: http://www.cs.ucla.edu/classes/spring15/cs33/lab/smashinglab.html If there is a unix command that requires your username, replace any "ericki"'s you see with your own username. ================================== ======== How to setup gcc ======== ================================== First, go to your home directory, and add the following line to your .bash_profile file: $ nano .bash_profile This opens up a very basic text editor (handy because it's usually always installed on all unix versions). Add the following line: PATH=/usr/local/cs/bin:$PATH Then, save the file (-o, then ), and exit (-x). Finally, logout, and log back in. Now, gcc should point to the right version: $ which gcc /usr/local/cs/bin/gcc $ gcc -v gcc version 4.9.2 (GCC) ================================================= ======== How to download+extract sthttpd ======== ================================================= First, we download the source code (in the .tar compression format) by using the handy curl command: $ curl -O http://www.cs.ucla.edu/classes/spring15/cs33/lab/sthttpd-2.27.0.tar.gz $ ls sthttpd-2.27.0.tar.gz Next, we decompress the .tar file: $ tar -xvf sthttpd-2.27.0.tar.gz $ ls sthttpd-2.27.0 sthttpd-2.27.0.tar.gz ================================ ======== How to compile ======== ================================ First, add the patch to the correct src file (see the lab spec for details). In particular, you will be modifying a few lines in: src/thttpd.c Then, within the sthttpd-2.27.0/ directory, do: $ ls aclocal.m4 config.status docs Makefile.am missing stamp-h1 config.h configure extras Makefile_ek README TODO config.h.in configure.ac install-sh Makefile_ek~ scripts www config.log depcomp Makefile Makefile.in src $ ./configure \ CFLAGS='-m32' \ LDFLAGS="-Xlinker --rpath=/usr/local/cs/gcc-$(gcc -dumpversion)/lib" It thinks for a few seconds, and spits out many lines. Note: If you're having trouble getting the above multiline to work for whatever reason (say, having trouble copy+pasting, or newlines are acting funny), then you can simply type out the command all in one line: $ ./configure CFLAGS='-m32' LDFLAGS="-Xlinker --rpath=/usr/local/cs/gcc-$(gcc -dumpversion)/lib" Once the configuration is done, we can compile each version of thttpd that the spec wants us to create: $ make clean $ make CFLAGS='-m32 -g3 -O2 -fno-inline -fstack-protector-strong' # it thinks awhile, then creates executable src/thttpd $ mv src/thttpd src/thttpd-sp (repeat for thttpd-as, thttpd-no) =============================== ======== To Run Server ======== =============================== First, create a test file within the sthttpd-2.27.0/ directory: $ echo 'this is a test file, high five' > testfile.txt $ cat testfile.txt this is a test file, high five Next, there are *two* different ways to start a server. Most students will be happy with the first (1) option. If (1) doesn't work for whatever reason, try the second (2) option. ==== (1) Using only one terminal instance ==== First, start the server with the following command (note we don't pass the -D flag): $ src/thttpd-sp -p 50000 This will return immediately. If the port is "OK" with seasnet, then your server will be running in the background. To check if this is the case, do: $ ps -u ericki 21577 ? 00:00:00 sshd 21579 pts/5 00:00:00 bash 25388 ? 00:00:00 sshd 25390 pts/13 00:00:00 bash 25590 ? 00:00:00 thttpd-sp 25593 pts/5 00:00:00 ps Here, we see that the thttpd-sp process is indeed running in the background! We can verify that it's working correctly by asking it to give us the testfile.txt file: $ curl http://localhost:50000/testfile.txt this is a test file, high five Success! Skip ahead to the next section to see how to shutdown/kill running servers running in the background. If the server is *not* running, then the port number is probably just not "working" right now (something to do with the seasnet servers). Instead, keep trying different port numbers (between 1025 - 65535) until it works correctly: $ src/thttpd-sp -p 50042 $ ps -u ericki # is it running yet? if not, then try another port# $ src/thttpd-sp -p 60032 $ ps -u ericki # is it running yet? if not, then try another port# $ ... If you're really unable to find a working port number, then post on Piazza and e-mail your TA. ==== (2) Using two separate terminals ==== First, note which lnxsrv you are on. For instance, if I see this: [ericki@lnxsrv01 ~/cs33/lab3/sthttpd-2.27.0]$ Here, I see that I'm connected to lnxsrv01. Next, start the server with the following command: $ src/thttpd-sp -p 50000 -D If all works well, then this command will appear to hang/freeze. This is good! If the command returns immediately, then the server is *not* running. Please try different port numbers until you get the terminal to appear to stop/hang/freeze. Next, open up a *new* ssh session (or terminal) into your cs33 seasnet account, but make sure to connect to the *same* server that we noted earlier: $ ssh ericki@lnxsrv01.seas.ucla.edu As a sanity check, check that the server is indeed still running+visible from your new terminal: [ericki@lnxsrv01 ~/cs33/lab3/sthttpd-2.27.0]$ ps -u ericki PID TTY TIME CMD 21577 ? 00:00:00 sshd 21579 pts/5 00:00:00 bash 25388 ? 00:00:00 sshd 25390 pts/13 00:00:00 bash 25879 pts/13 00:00:00 thttpd-sp 25884 pts/5 00:00:00 ps Here, we see that the server is running. Great! We can check to see if it's working correctly by asking it to give us testfile.txt: $ curl http://localhost:50000/testfile.txt this is a test file, high five To shutdown/kill the server, you can either kill the server by doing -C in the first terminal, or by using the "kill" command (see next section). =============================================== ======== Shutdown/kill running servers ======== =============================================== First, find the process ID (PID) of the running server: $ ps -u ericki PID TTY TIME CMD 21577 ? 00:00:00 sshd 21579 pts/5 00:00:00 bash 25371 ? 00:00:00 thttpd-as 25388 ? 00:00:00 sshd 25390 pts/13 00:00:00 bash 25452 pts/5 00:00:00 ps Here, we see that we have a server thttpd-as running, with a PID of 25371. We can shutdown this server by using the kill command: $ kill -s 15 25371 $ ps -u ericki PID TTY TIME CMD 21577 ? 00:00:00 sshd 21579 pts/5 00:00:00 bash 25388 ? 00:00:00 sshd 25390 pts/13 00:00:00 bash 25453 pts/5 00:00:00 ps Here, we see that we have indeed shutdown the server. ======================== ======== Outro ======== ======================== That's it for now. Feel free to ask questions on Piazza. Thanks! - Eric Kim (ekim@cs.ucla.edu) ==== Version History ==== v1.0 [5/6/2015] Initial creation. [CS 33 (Spring 2015, Prof. Eggert)]