Showing posts with label university. Show all posts
Showing posts with label university. Show all posts

Friday, February 10, 2012

Post-GSoC work, End(?) of University

I have done the seamless cloning project for GIMP and finished it in August. So, you may ask, why on earth is it not integrated yet? The answer is short and simple - last university semester combined with real life. But, I'm not here to bring excuses, I'm here to bring results:
  • On Thursday I had my last university exam (Computational Complexity). Unless I fail, and I hope I don't, I finished university!
  • In the last two weeks of university, I had more and more free time so I began working again on my GSoC project. The current things that are waiting to happen are:
    • Fix the triangulation refinement implementation. For those who are not familiar with the algorithm behind the scene, the algorithm itself is based on interpolation of image differences in a triangular mesh. Therfor, it is a critical part.
      The algorithm currently would get stuck in an infinite loop of refining the triangulation, even though it was gauranteed to stop (meaning an implementation bug). I think I just fixed it now (now as in 5 minutes ago), more on this below.
    • Make the algorithm iterate over tiles inside of allocating a big chunk of memory, and thus limiting the maximal image size for the algorithm.
    • Fix memory leaks (there are several known leaks, and hopefully none unknown).
    • Improve a small glitch on the borders of the image.
    • Optimize the speed!
    • Get it reviewed, and merge back to master.
  • Being free of university, and free from other work (I have a day work on which I don't work "after-hours"), I'm now free from chores on evenings (if I get home at sane hours) and weekends. For the first time in the last 3.5 years, I really have free time with nothing on my mind. So don't worry, I'll get into GIMP :D
Now, before I get into details on my GSoC bug, a short "what's going on?".
  • Last time I left the geometry library, I was completly stuck with no idea what was the bug.
  • When I got to it again, I decided to re-write it in a language which is more type-safe (no pointer bugs), with less memory issues (garbage collection), and much easier to debug (no offense, but gdb isn't always ideal). Eventually I chose C# (and not Java, my favorite language) in order to learn a bit.
  • I have been spending my free time between the exams, in the last two weekends, to re-write the library. The re-write was something like "look at the code and write it in a different language". Almost no structural changes or anything, so that I'll keep synced with the source.
  • And then I found the bug...
Now for the bug, which made me like C much less than I did before:

// Relevant type declarations:
typedef enum
{
  INCIRCLE_ON,
  INCIRCLE_INSIDE,
  INCIRCLE_OUTSIDE
} P2tRInCircle;

// Types from GLib
typedef int  gint;
typedef gint gboolean;

// The function I called
P2tRInCircle p2tr_math_incircle (P2tRPoint* a,
                                 P2tRPoint* b,
                                 P2tRPoint* c,
                                 P2tRPoint* d);

// Somewhere in the code, in the function that I knew that
// was deeply related to the problem:
gboolean inside = p2tr_math_incircle (p, a, q, b);

if (inside)
  {
    ...
  }

I don't even feel like explaining this. It is so trivial (and if not, go learn C!) Other than feeling stupid silly for falling on such a bug for days of debugging, I don't have anything else I can say... So, with all the dislike of some people to high level languages, I like type safety. It saved my here simply since this code, when converted directly to C#, didn't compile and produce an error instead!

So, Hurray for C#, Hurray for end of University (hopefully) at least untill second degree, and just some other random hapiness. And don't worry, I'm not gone yet :)

Edit: More silly bugs found. I'm starting to like re-writing stuff - way better than debugging...
Edit 2 (February 11th, 2012): The geometry library works, at least in C#. Now it's time to re-implement in C.

Friday, May 27, 2011

The end is near, and so is the beginning

Pictures from Yehuda Poliker's Concert on the
Student-Day, and a picture of my GSoC kit. Cellphone
pictures, so low quality :P
There is a feeling of an end in the air. The end of university, another chapter in my life. Yesterday I took part in the Student Day party at our university - there were amazing concerts by fantastic artists, including Yehuda Poliker, Monica Sex, Hadag Nachash and more. The Student Day is a known symbol of the end of the year, which happens 2-3 weeks before the end of the semester. The fact that is practically my last year as a student (I will have one last course left for the next semester, but that doesn't really count) does start to get in, and I am gradually getting used to this.

This week was also the official beginning of the Google Summer of Code coding period! After getting my cool GSoC kit (see the picture) 2 days ago I really felt that I'm a part of this great project - many thanks to google for organizing this! Anyway, I almost finished porting poly2tri to C. Once this is done, I can start coding my project. The milestones for my project are:
  1. Port poly2tri to C (from C++), a library for generating Constrained Delaunay triangulations for given polygons.
  2. Implement Chew's Second Algorithm to create adaptive meshes from CDT's*
  3. Implement code to create polygons from Black/White (1-bit) channels (The edges where the color differs should be described by polygons)*
  4. Combine all these to create an adaptive mesh from a Black/White channel (which symbolizes a selection area)
  5. Create a triangle mesh renderer with color interpolation between vertices (actually, Cairo may be able to do this!)*
  6. Implement the Mean-Value-Coordinate cloning, using these above, as a GEGL operation*
  7. Add some GUI and put it as a GIMP tool
Marked with * are the steps that would require the actual work, most other steps should hopefully be trivial. But again, hopes and reallity don't necessarily agree :) When I applied for this GSoC I knew there was code to do adaptive mesh generation from polygon outlines. It's only too bad that none of the code to do this is small and in a GPL compatiable license :(

I hope to finish everything untill step 4 (and parts of 5 if I'll be able to use Cairo) untill the midterm, but I can't really set the midterm goals yet since I haven't started to seriously code yet because there is still a university semester going on for the next week or two.

To all students out there, good luck in the exam period! And for the GSoC students, good luck with the projects! :D

Sunday, April 17, 2011

Safe programming - Are universities doing it wrong?

In the last 2 week I was unavailable because I had a “monstrous exercise” from university. Everyone indeed complained it was huge, but when other people saw the size of my code (2000 lines) they were shocked – this was because my code was about twice larger than the one of other people. It’s not that I suck at programming, and it’s not that I re-invented the wheel or anything like it. It was simply that my code was safe, where the code of most other people I saw wasn’t because we didn’t have to make it safe.

The assignment was to build servers and clients for a Nim game with several players (instead of just 2 like the original version) along with a build-in chat. What was supposed to be the point of the exercise was to make the server non-blocking, i.e. to use select() with only one thread. For example, this means that if we temporarily can’t read data from one client, we will still check if we can send him data or communicate with other clients. But let’s leave that technical part, since the thing that actually made the code complex was the other “minor” things.

Note: I have no criticism at all at the course staff of this specific course, It was simply an example of a university policy.

In general, when programming academic tasks, and especially when it’s in C, unless it is a course in security or some other special cases, you are allowed to assume the following assumptions:

  • Input from the user is valid (Less often than the rest of the assumptions, but still common)
  • We won’t run out of memory (in C, malloc() won’t fail)
  • Data received over the network is in the format we expected
  • No one will try to brake our system maliciously

Now, let’s analyze in how many places we can attack the server/client from our exercise:

  • Entering a wrong move to win the game
    • Example: Enter “A -5” instead of “A 5” to add 5 cubes to a stack instead of deducing (which is what that should happen)
    • Potential Result: Win the game by cheating
  • Sending large chat messages to attack client/server
    • Example: The protocol requests to send the size of the message to be read over the network. Most people used a short (16 bits) variable for that = meaning 65Kb message sizes are legal!
    • Potential results:
      • Buffer Overflows
        Most people allocated a static buffer of size 1024 bytes (1Kb) for receiving messages since someone asked if 1024 is a valid assumption on input length. Reading 65Kb to a one Kb buffer will crash our program or lead to execution of arbitrary code.
      • Wasting resources
        Allocating 65Kb for each client (to read chat message progressively until receiving them is done) is a lot of memory
  • Abuse the lack of timeouts on clients
    • Example: Connect with many clients, and do nothing after the connection was initialized.
    • Result: Can DDoS the system very easily, after finishing the maximal available number of connections, or waste all the resources if no such limit is set.
  • Send messages over the network in invalid format (can happen if client/server are broken)
    • Example: Send random binary junk
    • Result: see server crash or behave strangely
  • More…

Out of the above problems, I tried to take care off most of these problems, and I believe my partner and I took care of 90% of these cases. This is where we made our life harder – we didn’t have to handle any of these!

Now, obviously there were no requirements to handle these cases. This was done in order to allow students to focus on the subject of the task – non blocking network communication. And that is indeed something which should be done – otherwise you’d spend most of the time of the exercise (like me) on things which are barely relevant to the current subject.

So, after stating that we must take these assumptions, why am I writing this post? Because there is not even a single course in security and/or secure programming which is mandatory! Or at least this is how it is in my university (and I’m in a relatively respectable one). This means that a student can finish his entire degree without taking care of these problems even once! The implications of this can be devastating – if we code like we did in university, in the outside world, our programs will have more security holes in them that Swiss cheese!

Three weeks ago, I had to give a lecture (in a seminar) about common programming mistakes that make software unsecure. At first I thought most of the mistakes I was going to discuss were ridiculous, but then I saw them pile up in more and more “respectable” places – both in commercial companies (Apple, Microsoft, …) and open-source organizations. And like other programmers, I also do these even though I hate to admit it.

So, what can Universities do about it?

  • Changing the requirements of all exercises to be secure
    • Bad solution! The amount of work will make all students crash as if someone was DDoSing them :P
  • Mention the risk of each assumption inside each exercise description
    • Good!
  • Add mandatory courses/lessons in application security and/or safe programming
    • Better!
  • Instead of a one time experience during your studies (one mandatory course/lesson), have one task that should be safe in each course (where applicable)
    • Best!

And what can we do about it?

  • Try to show people we know were their code is dangerous, and increase their awareness.
    • If possible, try to attack their code and show them the actual risks! (because I heard enough people saying “The risk is only theoretical – nothing will happen”)
  • If you are a student, show this to the professors in the department of application security to make them tell the university to change it.
    • Some of them weren’t aware of the fact the state is so bad, and when they heard they started working to integrate the solutions mentioned above
    • But this is less likely to work…
  • Make our own code safe!

That’s all for today. Spread the awareness about security, and if you (as a student/programmer) found this post helpful, please let me know.

One last thing – I applied to GSoC (before the deadline), I’ll post my application later :)