Singpolyma

Dystoparx — Part 17

Posted on

Child Abuse Right at the Top?

Yesterday, RCMP Comissioner Robert Sandall was suspended on suspicions raised by an internal investigation. No details have been released, but a source close to the matter has told the CBC that Sandall may be implicated in several cases of direct child abuse and possibly the creation and distribution of child pornography. No charges have yet been filed.

Watch this space for updates as the situation develops.

Acklas pulls out his laptop and logs on. He has taken a bus to a neighbouring city and is in a coffee shop there, hoping no one will bother him to buy something before his task is complete. Everything that needs doing has been scripted in advance. He runs the script and watches the output, hoping that everything works on the first try. To his suprise, it does. He smiles. Good, everything is in place. He double-checks a couple of things to make sure the script has indeed done its job. It has.

He closes his laptop and waits a few minutes before heading out. No one bothers him.

Acklas boards a bus heading back towards home. He waits several more minutes before putting the battery back in his cell-phone and connecting to a chat with Jack and Nicnus.

* Your connection has been secured

17:19 <acklas> on to phase 2 😀

17:21 <jjdavis> phase 2 of what?

17:22 <nicnus> jjdavis: of his plan to take down All the Bad Guys

17:23 <acklas> nicnus: 😛 yes, sure

17:23 <jjdavis> what does phase 2 entail?

“What do you mean, it’s gone?” Bill gives the tech a smouldering stare.

“Well, uh… obviously we still have any data we saved out of the stream as it came in, but—”

A second tech breaks in, trying to save the first from some embarassment, “But nothing is coming in anymore. That source is sending literally no data.”

Bill considers this, then calms down, “That’s expected. The Comissioner has been suspended. He knows we know. He would stop anything that was happening.”

A moment, then, “Sure, but—”

The first tech breaks back in, “We called the MusicBox people. They have no record of any of the data. At all.”

Bill considers this. No record at all? But… it’s their data. All the data comes from the MusicBox people… “How is that possible?”

“We don’t know.”

Someone is calling his name. He looks around. A head pops up from another area of the office and calls him over.

They’re showing him a YouTube video. What? A YouTube video? Something is happening, he—

“Shh.”

Clouds are rolling on the screen. A storm. Some kind of nature video? Then the voice begins.

Chilling.

It’s clearly just a cheap computer voice, and yet… paired with the visuals this computer voice sounds like… well…

“Good morning,” it begins, flubbing the intotation already just this far into the speech, “When the RCMP and other law enforcement agencies first got excited by the idea of circumventing privacy law by taking the data being collected by the MusicBox software and using it for their own purposes, many experts pointed out that flaws in the system could allow false data to be injected into the stream. Some of these weaknesses were exploited to produce an anti-spyware pseudo-virus, which now lives on many of your computers, protecting you from the prying eyes of those who would read your private data.

“Yesterday, I proved a practical exploitation of a much more sinister weakness. I sent false evidence of child pornography creation and distribution to the RCMP, and only to the RCMP. No one else has this data. On the basis of this data and this data alone (for there could not be any other source, especially given the timing), the RCMP suspended their own Comissioner and began an internal investigation.

“The code which I used to do this has been published at the location linked to in the description of this video. It is so simple, and yet the most important law enforcement agency in Canada started a witch hunt after just one day of its use. Imagine what would happen if they were given the power to exploit other, even less reliable, data streams? It would be just like the French Revolution: accusations flying wherever they will, and law enforcement blindly following along with everything they are told.

“I have stopped the transmission of the false data. I hope the law enforcement officers, the politicians, and the media, will see this as a sign that they cannot trust the data they have been trumpeting as their salvation. No real arrests have come of these projects, and now it is obvious that many false arrests may be their eventual result.

“If, however, this warning goes unheeded, I have no doubt that many others like me will rise up to make chaos. Too long have we sat aside while our governments slide into a pit. They have declared war on us the people, and if it is war they want, then we will bring the chaos. We are Anonymous. We are Legion. We do not forgive. We do not forget. Expect us.”

17:50 <jjdavis> Really, acklas? An *Anonymous* video?

* acklas shrugs

17:51 <acklas> seemed like a reasonable way to communicate

* nicnus shakes his head

17:54 <acklas> either way, the ball is now firmly in their court. I may not even have to do anything more

17:55 <nicnus> acklas: you have way too much faith in the Internet Hate Machine. They get bored easily

17:55 <acklas> maybe

Later that day, Nicnus’ brother also sees the video. He shows it to his girlfriend. They have no idea. His girlfriend, however, is disgusted. She supports the legitimate actions of law enforcement to bring an end to all that is unwholesome. Or something like that.

She’s watching her favourite talkshow. Or whatever this sort of show is being called by her social group. The video has become a topic. Acklas has done a very good job of blanketing the media in awareness. Lots of messages went out. Once the mainstream is reporting it at all, then they all have to report it, or look like they’re behind. The show’s host is asking for submissions on her website, to get feedback from “you out there” on what people think of the situation. There will be a summary on tomorrow’s show.

She flips open her laptop, navigates to the website, and begins a submission.

Well, I think it’s disgusting! here’s the police, trying to do something good, get rid of the weirdos that ppl the entire internet, and what does this guy do? he just makes their job harder! stupid. i dont care if what their doing can be faked…everything can be faked…he needs to get over himself and they need to arrest him so they can get on with their jobs, maybe if we give the police the ability to collect all the stuff from the internet, they can track this guy down and make ppl like him stop and then we can finally be safe!!

She slaps her laptop closed and walks off. Nicnus’ brother sits by his computer, thinking about hiding. She’s in that mood again. Ah, well, it’ll pass. He mouses around his system a bit, trying to find the program his brother prefers he use for chatting. Normally he would just text him, but his brother wouldn’t get back to him for hours in that case. He wonders breifly if Nicnus has got Facebook, but no, still no Facebook for Nicnus. Oh, there’s the program. Pidgin. What a stupid name. They should call it “geeky messaging application”. Actually, maybe he can rename it that, would make it easier to find…

He opens a chat with Nicnus and pastes a link to the video. Suddenly another window pops up. A chat with more than one person. Looks like his brothers’ friends.

* This chat is not secure

21:50 <brann> Woah, what is this?

21:52 <acklas> we were already discussing the video, so nicnus just brought you in here 🙂

21:53 <nicnus> brann: What did you think of the video?

21:53 <brann> g/f hates it.

21:54 <nicnus> suresure, would expect that

21:56 <jjdavis> nicnus: you rag on his SO too much

21:57 <nicnus> maybe

21:57 <brann> It’s a bit creepy.

21:58 <nicnus> brann: which is?

21:58 <brann> Well, all of it

21:58 <brann> the video is… but I think that’s more just a ploy or whatever

21:58 <brann> but, I don’t like the cops faking data for themselves either

22:00 <acklas> Who said the cops would fake data for themselves?

22:01 <brann> no one, but… seems the next logical step, no?

22:01 <brann> Not enough evidence on this guy? No problem, just make some and shove it in here. done

22:02 <jjdavis> holy crap…

22:03 <acklas> yeah. brann: I hope no one *else* thinks of that 😐

Jack is relaxing. His girlfriend is over, and she’s bubbling happily. He only sort of hears what she says. He’s just happy. Nothing has happened to him yet. If they were going to come after him, wouldn’t something have happened? Maybe not. Still, he’s happy for now.

More and more datacentres are finding the same sort of activity he found. One by one, they’re turning it off. If the FBI is snooping on their population, they won’t be for much longer. At least, not in the same way.

On a whim, he browses some of the groups where discussion is happening around the efforts to stop what most people are calling an ‘exploit’. One message in particular interests him. It seems that, hidden in the guts of some Omnibus bill about kittens, there is a section dedicated to increasing the online ‘wiretapping’ abilities of the FBI and other federal agencies. The mechanisms outlined are very suspiciously similar to what is already happening. If it becomes law, though, it won’t be hidden. The datacentres will be ordered to install this software on purpose.

His girlfriend has stopped talking. She’s looking over his shoulder. Wondering what’s up.

“Uh…” Will she even get it? If she does, will she care?

It doesn’t matter. He’s trying really hard. He’s not making assumptions about that anymore. He’ll tell her what’s going on.

He starts at the beginning, in the datacentre. He hasn’t told her before because it didn’t seem important, in light of their relationship trouble. Well, sure, maybe he should have mentioned it before now. No, nothing bad has happened yet. Maybe nothing will. Maybe it will become legal, he’ll be forced to re-install the software, and he can just pretend that’s it.

Yes, he does have a problem with what it seems the FBI is doing, even if it becomes legal. No, there’s nothing much he’s going to do about it. Why not? Because he’s a guest in this country and he wants to stay here. Because she’s here. Right, she’s not that interested in leaving. But can’t something be done? Spying seems icky. Yes, that’s her word, ‘icky’. Fine. Maybe. He’d really rather not stick his neck out and find out.

Well, wait and see for right now, that’s the plan. Maybe nothing will happen.

Dystoparx — Part 16

Posted on

16:00 <jjdavis> So, we’re all secure now?

16:02 <nicnus> Yup! As long as the devices are on and we use this chat system, everything is being encrypted.

16:03 <jjdavis> That *was* pretty easy. I guess this is a good idea.

16:04 <acklas> 😀

Acklas hits a key combination and is again staring at a screen full of source code. Someone had indeed already worked out what would be needed to spam the RCMP with bogus data, but just being able to shove data down the pipe is not enough. Acklas certainly doesn’t need this backfiring on him. He is not going to run it on his own computer, or over his own Internet connection, and all the traffic needs to be masked no matter where it is he ultimately runs this. Safety first.

He quickly scans over what he’s been writing, makes a quick change, and then tries running the system again. He’s just running it locally to make sure it works, no data is actually being sent.

16:10 <acklas> Ok, I’ve got the MusicBox blocker patched to send whatever data I want

16:10 <acklas> also, it has a backdoor so I can remote control it no matter where it’s running

16:12 <acklas> and I’ve got it working so that it will send all traffic (even DNS) through a given SOCKS proxy

16:14 <nicnus> Tor?

16:15 <jjdavis> good call

16:15 <acklas> yes

Quickly, so that the reader is aware, “Tor” (or TOR, The Onion Router) is a software designed to help whistleblowers and those in oppressive nations use the Internet without fear. It wraps data in many layers of encryption and then bounces it off many different computers, in a way that makes it virtually impossible to tell what computer originally sent the data.

16:17 <jjdavis> Where are you gonna pit it, though?

16:17 <jjdavis> s/pit/put

16:18 <acklas> I’m thinking of renting some space with BTC

16:18 <nicnus> botnet?

16:19 <nicnus> hmm… that’s probably safer

16:19 <nicnus> cool

Acklas begins reading over the list he had brought up earlier of Internet hosting providers that will accept Bitcoin (abbreviated BTC), the pseudonymous digital currency he plans to use as a way to keep his name off of any space he rents. Credit cards and other forms of payment, other than cash, have a tendency to have one’s name attached to every transaction, which is a bad idea if the plan is to send a lot of bogus data to the RCMP, regardless of how you feel about what he is trying to do.

He finds some that seem promising and saves them in a file on his laptop, then packs up to move to the public library. He only lives a few blocks away, so he walks. You may think that such a close library will make it fairly obvious, still, where he lives, and you would be right. Anyone who can trace what he is about to do to the library will have a much smaller pool of people to choose from than they would if they could not trace the connection. However, he has no library card, and does not need one to get online. At a coffee shop, he would likely need to make a purchase to get online, and that’s just that much more data he’d be leaving behind.

Once online, he sets up his Tor client to further mask the source of his data. He scans the list of Internet hosts he has drawn up and keys in an email to each one, asking for some details and requesting that they reply by emailing certain email addresses he has selected at Mailinator, the free service that provides disposable email addresses to anyone. Why does he have to ask for them to email these disposable addresses instead of just replying? He is sending the emails through a service called “Mixmaster” which hides the email address of the sender. The people receiving these emails won’t be able to tell who Acklas is or where he was when he sent the email, yet they can still reply.

If they are willing to do that, then he will know they are the right sort of place for what he has in mind.

Bill is happy. He’s not sure what the exact status of the politics are, but the media seems on board. The most recent operation was just a start. They need more data. Whatever it takes to stop the abuse of children. Whatever it takes.

There is a knock at his office door. He does not hear it. He is, of all places, on Wikipedia, reading about Internet technologies. He’d really prefer some other source, but this seems to be the only place with the information he’s interested in. If they’re going to take down the pornographers, he needs to have some understanding of what’s possible. It’s no good asking for data they cannot possibly get, or not asking for data that would be easy.

Another knock, and the door opens. One of the cops who works with him. One of the more technical ones. Under him. With him. Whatever, the distinction is lost on Bill most of the time. Apparently there’s something interesting they’ve found in the data. Something worth seeing.

Bill is lead to a small meeting room. Others are already there. They wanted to be more sure before presenting this to him. They are showing him data. Some of this data is disgusting. More explicit than anything they’ve yet uncovered. As he is shown more and more data Bill is wondering, can they catch this guy? What a windfall!

It turns out they’re way ahead of him. He has a good team. They traced the traffic, but they didn’t really need to. This is the good stuff. The user was logged in to MusicBox and everything when the pornography was downloaded (and uploaded! A source!) They have his name, his credit card information, his address. Everything.

Oh.

Oh.

“You did WHAT?”

Acklas is smiling. It has been a good few days. “I set up some services to send fake data through the MusicBox service.”

“Yes, I got that part,” Nicnus looks around worriedly. They are in private, but this sort of discussion makes him even more paranoid than usual. No one out the window, at the least. “Repeat the other part.”

“I set one of them in motion streaming fake kiddie porn downloads and uploads for the RCMP Commissioner.”

Nicnus blinks. “You did WHAT?”

“I think we’ve been here already.”

Nicnus pauses for a long moment, then, “Are you crazy?”

Acklas shrugs, “Quite possibly. Still, I was very careful. The traffic should be untraceable, and even if they find the box it isn’t tied to me.”

“No, I know,” Nicnus stares at the wall for a bit.

…

“It seems reckless.”

“Yes.”

“Then… ?”

Acklas sighs. “I’m just sick of it all. Writing MPs, signing petitions, reading proposed law, dealing with my boss, going to meetings—”

“You can’t blame your crappy ex-job—”

Acklas shakes his head, “No, I know. Look. I know. I just mean. Yeah. Hoops. No more hoops. I’m taking the direct approach.”

Nicnus sucks on his teeth a bit. This is a hard one. Nicnus also hates jumping through hoops. He also hates the feeling of powerlessness the current political climate gives him. He hates seeing the world slowly and willingly slip into madness. This is how he feels, and he understands how his friend feels. Nothing, however, is airtight. There’s a huge risk in this. What will this accomplish anyway?

He verbalizes that last thought.

Acklas is about to respond when both their phones go off.

piratepartyca: BREAKING: RCMP Commissioner temporarily relieved pending investigation.

Nicnus raises an eyebrow at Acklas.

Acklas looks up and meets his gaze. “I was going to say that what happens depends on how they respond, but I guess we know that now.”

Nicnus is shaking his head already, “You know you may have destroyed an innocent man’s life, right?”

Acklas holds up one hand in a defensive gesture, “Woah, I’m not just going to leave him! I’m going to tell them what I’ve done.” He sees Nicnus about to make a response, “Anonymously, of course. I’m being careful. Isn’t this exactly the idea you brought up when we first saw mediacrack?”

Nicnus thinks back. That seems so long ago. “Yes. I suppose it is. I didn’t expect anyone to do it, though. That’s why I built a blocker instead.”

“I know. But this way they’ll have to see how unreliable the data is. After their disaster operation recently, and then this, they’ll have to see how futile this is.”

Nicnus is thinking. It can’t be that easy. “The data is only unreliable if people keep sending bad data. They’ll just focus their attention on you and anyone else trying to do this.”

Acklas sighs. “Maybe. And maybe they shut some of us down. And then someone even shadier than me puts it on the botnets and then shutting it down becomes as hard as fighting SPAM. Believe me, I questioned the ethics of this myself, but someone has to do it. This has all gone on far too long. I will push them until they break. The whole frigging media industry, every moron bureaucrat at every level, if it comes to it. We can’t just keep hoping that letters and petitions and books and blog posts will eventually make them see the light. The time for polite suggestions is over. They need to be stopped.”

Nicnus shifts uncomfortable, but gives in, “I think it’s very risky, and I’m much less convinced it will be successful than you are, but you’re right about one thing: the whole situation has gone on far too long. I wish you luck.”

Their phones once again buzz as one. It’s time for them to head out towards the meetup they are attending tonight.

Jack is at home again. Rested. Worried. Rested and worried. Now that he’s had some time for everything to sink in, he’s begun to realise the full import of his situation. He can’t just ignore what he found, because now others are finding it too. Based on his reports online, several other systems and network administrators have identified the same spying going on in their datacentres. Some of them have been much more ruthless in getting rid of it. Others have also traced it to variously the FBI, the US military, or the Whitehouse. Jack’s guess is that the IP blocks overlap quite a bit. What this does indicate, however, is that it’s not a security breach at the US government. They’re monitoring on purpose. And it’s illegal.

Sure there are all sorts of debates going on in Congress right now about how much monitoring should be allowed, but none that have gone much of anywhere. Unless the FBI has just decided to treat everyone they tap as terrorists… but they seem to be tapping indiscriminately.

Jack hopes the wealth of reports will take the heat off himself. Maybe some of these more rash admins will get the call.

He has an appointment set up with a lawyer. Just in case he gets the call and it’s the sort of call where they let you talk to a lawyer. He wants to know one. And have the phone number tattooed on his arm. He is also considering some sort of dead-man’s switch to notify people if he disappears unexpectedly.

One thing is for sure. He’s not leaving.

Haskell for Rubyists

Posted on

In the last year I’ve been playing with a new language: Haskell. I have found it to be a very suitable second high-level language for me as a Rubyist, and in this post I will explain (with examples!) some of why I, as a Rubyist, love Haskell, and how you can use it to do awesome things.

Why Another Language?

One thing I wasn’t sure about for a long time was if I even needed another language. Obviously, being able to work in any environment you have thrown at you is an essential job skill, but I mean a language I chose for myself. I am a Rubyist, do I need to be anything else?

Well, while I love Ruby, there are a few reasons I eventually chose to go in search of a second language to call my own:

  1. Performance

    Ruby implementations are getting faster all the time, but we all know there are faster things out there. It’s the reason some things (like the JSON gem) have versions written in C. I felt it would be nice for some tasks to get the performance boost that comes from an optimising compiler, without having to drop all the way to C.

  2. Portability

    Yes, Ruby is super-portable… to systems that have a ruby implementation. It’s somewhat complex to just email arbitrary users a ruby script and hope they can use it without setup help.

  3. Linking with native code

    Ruby extensions and FFIs exist so that we can call C code from Ruby. What about calling Ruby code from C or another language? It can be done, but only if most of MRI is linked in to the target and the Ruby is more-or-less “eval’d”.

In case you haven’t guessed, basically I wanted a nice high-level environment like Ruby, but with a native code output.

Isn’t Haskell Hard?

No. Or at least, not harder than any other language. It is true that the Haskell community has a higher-than-average concentration of Ivory Tower Dwellers. Yes, some of them have been in the Tower for so long that they have forgot how to write anything but symbols from higher-order logics. Yes, the documentation for some really nice Haskell libraries and features are dense academic papers. Don’t let them scare you off. There are humans in the community as well, and # on freenode IRC has many of them.

Type Inference

One of the nice features of Ruby is the type system. If you’re used to un-inferred static typing (read: C) then the ability to write code like this:

def fun(a, b); (a + b) * 3; end

is liberating. Haskell has a static type system, which means that you’ll never have a program crash in production because you’re passing in different data than you though, but only in a case your tests didn’t catch. Unlike C, however, Haskell’s system is strong (which means that data is not magically cast for you, so you get stronger guarantees, just like how in Ruby we must write 1.to_s + "hello" not 1 + "hello"), but more importantly it is inferred, so the equivalent of the above in Haskell is:

fun a b = (a + b) * 3

You can add type annotations (like in C) if you want to, which sometimes helps for clarity, but you don’t need to.

The only limitation here is that data structures are mostly of a single type, for example in Ruby:

a = [1, "hello"]

is perfectly fine. This is sometimes a good thing, and sometimes causes strange bugs. In Haskell, this would be an error, so we need to define unions explicitly:

data StuffInMyList = I Integer | S String
a = [I 1, S "hello"]

A small pain, but I feel it’s a fine trade-off.

Mixins

The mixin module is one of the defining characteristics of Ruby. Haskell has something similar, called Typeclasses, which form the foundation of polymorphism in the language. In Ruby:

module Equality
def equals?(b); self == b; end
end

class Thing
include Equality
end

In Haskell:

class (Eq a) => Equality a where
	isEqual :: a -> a -> Bool
	isEqual x y = x == y

data Thing = Thing deriving (Eq)

instance Equality Thing

This looks a bit different. You’ll note I had to give a type signature to the isEqual function. This is one of the few places you have to, and it has to do with making the polymorphism we get with mixins a bit safer. My Equality mixin has to be restricted to types from the Eq typeclass (because I use == on them), which is also true in Ruby except that in Ruby every single class has == defined.

Significant Whitespace

Haskell has significant whitespace. If you’re a Rubyist on the run from Python this may scare you, but there are two reasons this does not bother me. First, the Haskell whitespace is much nicer than in Python, and the way code gets written in Haskell you rarely have the “where does this huge block end?” problem. Second, the whitespace in Haskell is optional! Here’s that typeclass again, but without the whitespace use:

class (Eq a) => Equality a where { isEqual :: a -> a -> Bool; isEqual x y = x == y; }

Great!

Let’s see a real example!

You may have heard that Haskell I/O is weird, and that Haskell has no access to mutation. While Ruby code is often non-destructive in nature itself, access to mutation is sometimes handy. Understanding why Haskell I/O is safe and such is not terrible, but it does take learning a new concept (called Monads, with roots in those academics, but there are good simple explanations out there without too much math, like in Learn You a Haskell (for Great Good), which I recommend), but doing simple I/O is actually not complicated.

main = do {
text <- readFile "somefile.txt";
print $ length $ lines text;
}

This is the Haskell code to read a text file, split it in to lines, count the number of lines, and print out that number. Pretty simple!

What about mutation? Well, it is true that there are no globals in Haskell, but really, who uses globals? If you really need mutation for something, the simplest way to make a reference is:

import Data.IORef

main = do {
someRef <- newIORef 1;
val <- readIORef someRef;
print val;
writeIORef someRef 12;
val <- readIORef someRef;
print val;
}

Of course, if you want you could make this a bit less verbose:

import Data.IORef

x := y = writeIORef x y
new x = newIORef x
get x = readIORef x

main = do {
someRef <- new 1;
val <- get someRef;
print val;
someRef := 12;
val <- get someRef;
print val;
}

Many Libraries

Haskell has a very active community that has produced many libraries covering all sorts of cases. The main place to look for these is Hackage.

REPL

Another thing that drew me to Ruby initially was irb. The ability to just fire up a shell-like environment and enter expressions, and load in my code and play with it live, is a very nice thing. There are several such environments for Haskell, the one that I prefer is GHCI, which also has commands to set breakpoints and such (which I have never needed) and to find out what the type of some expression is (very handy).

Other Useful Bits

There is a very useful tool for Haskell called hlint, which analyses your code and make (sometimes surprisingly insightful) suggestions. I don’t always agree with it, but it is very nice.

Debug.Trace is a very useful library for printing out arbitrary values from anywhere in your code without otherwise affecting the behaviour of the code. Very useful for debugging.

If you want to learn more, I highly recommend Learn You a Haskell for Great Good.

Writing a Simple OS Kernel — Part 5, Hardware Interrupts

Posted on

Last time we got the kernel switching back and forth between multiple tasks. This time we’re going to get a simple timer to wake up the kernel at fixed intervals.

Motivation

So far we’ve been interacting with hardware when we want to. Writing to the serial port when we have something to print, for example. Why would we want to let the hardware interrupt what we’re doing?

A few reasons:

  1. Currently, our user mode tasks have to call a syscall on purpose in order for any other task to be able to run. This means one task can run forever and block anyone else from running (called starvation). So we at minimum need some way to interrupt running tasks (called preemption).
  2. The hardware knows when it’s ready. It is way more efficient to let the hardware say “I’m ready now” then to keep asking it.
  3. The hardware may actually stop being ready by the time we get around to talking to it again. We want to talk to the hardware during the window when it’s able to talk

Enabling a Simple Timer

Before we get to the serial port, there is an even simpler piece of hardware we can work with first: timers. Timers just take a value and count down to zero, sort of like an alarm clock.

I could make you go look up in the user guides what all the magic numbers for our timers are, but I’m a nice guy, so here’s the ones we need to add to versatilepb.h:

/* http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html */
# TIMER0 ((volatile unsigned int*)0x101E2000)
# TIMER_VALUE 0x1 /* 0x04 bytes */
# TIMER_CONTROL 0x2 /* 0x08 bytes */
# TIMER_INTCLR 0x3 /* 0x0C bytes */
# TIMER_MIS 0x5 /* 0x14 bytes */
/* http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/Babgabfg.html */
# TIMER_EN 0x80
# TIMER_PERIODIC 0x40
# TIMER_INTEN 0x20
# TIMER_32BIT 0x02
# TIMER_ONESHOT 0x01

Now add the following somewhere near the top of your main:

*TIMER0 = 1000000;
*(TIMER0 + TIMER_CONTROL) = TIMER_EN | TIMER_ONESHOT | TIMER_32BIT;

The timer on our QEMU system defaults to using a 1Mhz reference, which means that it ticks 1000000 times per second. So, by setting it’s value to 1000000, it will reach 0 after one second.

Then, on the second line, we enable the timer, set it to “oneshot” mode (that is, it will just stop when it’s done counting, we’ll see another mode later), and set it to 32BIT mode (so that it can hold really big numbers).

Now, somewhere in your while loop, put this:

if(!*(TIMER0+TIMER_VALUE)) {
   bwputs("tick\n");
   *TIMER0 = 1000000;
   *(TIMER0 + TIMER_CONTROL) = TIMER_EN | TIMER_ONESHOT | TIMER_32BIT;
}

We’re just printing out “tick” if the timer has reached 0, and then resetting and re-enabling the timer.

Build and run your kernel. It should still work as before, but now it will also print out “tick” once per second.

Back to the Vector Interrupt Table

The CPU uses the same mechanism for hardware interrupting operation (often referred to as an “interrupt request” or IRQ) as it used know where to jump when we used svc. We need to add an entry to the right place. Here’s the new code for bootstrap.s:

interrupt_table:
ldr pc, svc_entry_address
nop
nop
nop
ldr pc, irq_entry_address
svc_entry_address: .word svc_entry
irq_entry_address: .word irq_entry
interrupt_table_end:

IRQ entry point

Back over in context_switch.s, irq_entry is going to be very similar to svc_entry, but with some changes to handle an IRQ instead of a syscall.

As soon as we enter system mode, we need to push the value of r7 onto the task’s stack (because that’s what the syscall wrapper does, and we need to set things up to look the same). The syscall puts the number identifying the syscall into r7, so we should come up with something that will identify an IRQ and put that there instead. To make sure the number never clashes with a syscall number, lets use negative numbers:

push {r7} /* Just like in syscall wrapper */
/* Load PIC status */
ldr r7, PIC
ldr r7, [r7]
PIC: .word 0x10140000
/* Most significant bit index */
clz r7, r7
sub r7, r7, #31

That value at PIC is the address of the interrupt controller status (that is, the value that can tell us which interrupts are currently firing) and then we just do some operations on that value to find out what the most significant bit set in the value is, and transform that into a negative index to use for the identifier of this IRQ.

The only other weird thing is that we have to get the value of the lr from IRQ mode instead of Supervisor mode, and the value is set to the instruction after the one we should be returning to, so we’ll need to subtract 4:

msr CPSR_c, # /* IRQ mode */
mrs ip, SPSR
sub lr, lr, # /* lr is address of next instruction */
stmfd r0!, {ip,lr}

So those bits, together with what we already had from svc_entry give us this complete entry point code:

.global irq_entry
irq_entry:
	/* Save user state */
	msr CPSR_c, # /* System mode */

	push {r7} /* Just like in syscall wrapper */
	/* Load PIC status */
	ldr r7, PIC
	ldr r7, [r7]
	PIC: .word 0x10140000
	/* Most significant bit index */
	clz r7, r7
	sub r7, r7, #31

	push {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,fp,ip,lr}
	mov r0, sp

	msr CPSR_c, # /* IRQ mode */
	mrs ip, SPSR
	sub lr, lr, # /* lr is address of next instruction */
	stmfd r0!, {ip,lr}

	msr CPSR_c, # /* Supervisor mode */

	/* Load kernel state */
	pop {r4,r5,r6,r7,r8,r9,r10,fp,ip,lr}
	mov sp, ip
	bx lr

A Small Problem

We’re pushing the value of r7, but where is it going to get popped? The syscall wrappers do both the push and the pop, but in the case of an IRQ we’re pushing in the entry point, and we have no easy way to tell activate that it’s exiting from an IRQ instead of a syscall. Hmm.

The solution to this that I’ve chosen is to remove the pop instruction from the syscall wrappers, and add an identical instruction to right after the pop instruction already in activate. Now the pop always happens.

More Magic Numbers

We hardcoded a single magic number into our assembly, but luckily that’s the only one our assembly code will need. In fact, we should not need any more assembly! (Unless we wanted to implement other sorts of hardware exceptions, which we may get to.) We do need that and a few other interrupt-controller related magic numbers in our C code too, so here’s the stuff for versatilepb.h:

/* http://infocenter.arm.com/help/topic/com.arm.doc.dui0224i/I1042232.html */
# PIC ((volatile unsigned int*)0x10140000)
# PIC_TIMER01 0x10
/* http://infocenter.arm.com/help/topic/com.arm.doc.ddi0181e/I1006461.html */
# VIC_INTENABLE 0x4 /* 0x10 bytes */

Enable the Interrupt

We have to tell the timer to generate interrupts, and we have to tell the interrupt controller to pass those interrupts through. So, replace your current one-shot timer code with this:

*(PIC + VIC_INTENABLE) = PIC_TIMER01;

*TIMER0 = 1000000;
*(TIMER0 + TIMER_CONTROL) = TIMER_EN | TIMER_PERIODIC | TIMER_32BIT | TIMER_INTEN;

Note that we’re also using a new timer mode here. Periodic mode will automatically cause the timer to restart from the value we set when it hits 0, which is perfect for when we’re getting an interrupt instead of waiting until the value hits 0.

Handle the Interrupt

Now we have to add a case to our switch statement for this request:

case -4: /* Timer 0 or 1 went off */
	if(*(TIMER0 + TIMER_MIS)) { /* Timer0 went off */
		*(TIMER0 + TIMER_INTCLR) = 1; /* Clear interrupt */
		bwputs("tick\n");
	}

The way our hardware is set up, the interrupt is actually the same for either timer 0 or timer 1. Now, we know that we’ve only set timer 0, but that’s lazy. TIMER_MIS is the offset where we’ll find a flag telling us if Timer0 was the one that is causing the interrupt. After checking that, we set a value on the timer to clear the interrupt (basically to say that we’ve seen it so that it won’t call us again unitl the next interrupt). Finally, print out “tick”.

As it is, this code should operate exactly the same as the version without the interrupts.

Pre-emption

Up until now, we’ve needed the dummy syscall syscall in an infinite loop at the end of every task in order to let other tasks run. Since the timer interrupts the tasks now, we don’t need them to interrupt themselves. Remove all references to the syscall syscall and try running the kernel again. Now you’ll note that the first task just stays in its infinite loop and does not let the other task keep running. Does not, that is, until the timer goes off and you see “tick” printed for the first time, after which the tasks switch. So the tasks are now being properly interrupted even if they do not give up control!

We’re done!

The kernel now has all the machinery it needs to be able to handle hardware interrupts, and tasks are being pre-empted by the timer. Next time we’ll start looking at IPC.

The code so far is on GitHub.

Dystoparx — Part 15

Posted on

Jack is outside her apartment. He slipped into the building behind someone else so that he wouldn’t have to push the buzzer. Now he’s just standing here.

He left the datacentre mostly under control. One of the ops came in and got a run-down of what’s going on. Just keep the place running, that’s all the op has to do. The attack should be basically harmless at this point.

Well, harmless to the datacentre.

Jack’s name is now directly associated with the attack as “the guy who first reported it.” Not good if his suspicions are correct. Maybe Nicnus is right, maybe he should—

Her door opens. It’s her. Of course, who else would it be?

She seems a bit surprised, “You came?”

“Uh…” He’s not sure he was being expected, but, “Yes?”

She smiles a sort of sad smile, then, “Come in.”

He comes in. Not sure where this is going. Maybe her friend told her something? Maybe he’s caught in some sort of unclear set-up? Best to just wait it out for now. So far she’s not unhappy with him.

Now he’s sitting in her living room. She’s in the kitchen. Probably preparing beverages. As ridiculous as it sounds, in this particular case he likes it. It’s a welcome form of procrastination.

He feels his phone vibrate. No. His attention is on her now. It vibrates again. No.

She is back with the beverages. On the coffee table. Now she is across from him, sipping. Looking expectant. Crap. He probably should have had a plan. Or… he thinks back over his conversation with her friend and decides to try something.

“So… I have absolutely no plan here.”

She says nothing, but at least she does not exhibit any signs of being upset by this. He decides to sip his drink and wait. The mug contains some sort of tea he could never get the name right for. It’s her favourite, and really not his sort of thing. He sips anyway.

His phone vibrates again. He is still ignoring it. Sipping. Willing his mind not to drift elsewhere. Here is where he needs to be right now. All of him.

“I’ve missed you.” She says it like it should be obvious.

“I thought you didn’t want to see me.”

She shakes her head in disgust, “That’s always been your problem.”

He raises an eyebrow at her and says nothing. Either an explanation is coming, or else he’s going to have to make it more obvious that he needs one.

“Why did you think I didn’t want to see you?”

He sips for a few moments, as if he is thinking. She needs to see him thinking. Finally, “You said as much.”

“Did I?”

He nods.

“Probably. Sounds like something I might have said. Your problem is that you take everything at face value.” She tosses her hair and continues to sip her tea.

He’s quite impressed. They’re having what is effectively a mostly-rational discussion about what happened. He never would have expected this. As he takes his next sip he realises something: this is his problem. He doesn’t give her enough credit. That she should be discussing the situation with him in a rational manner should not be impressive.

Yet…

Nor should it be expected. Huh.

“Because you didn’t want to see me then.” He says this slowly, as it comes to him, “But you didn’t want to have to tell me when that changed. Didn’t want to, because it seemed obvious that it would change somewhat quickly. You thought I should know that you would eventually want to see me.”

She is saying nothing, but her eyes say all he needs to know. His phone vibrates again.

“I know I can be hard to handle,” he pays his phone no mind, “I mean, I don’t take the world lying down. I expect clarity in communication. I want to correct errors wherever I see them—” He cuts himself off, a list of his faults is not going to help here. She knows what her problem is. “I can relax all that,” He’s sure that he can, “I mean, I can be more understanding. I can try to read into what you’re saying just a little more. Let trivialities be trivial a little bit more often…”

He doesn’t want to say what he has to say next. Things are going so well. He is realising, though, that as much as he wants this to work, it is not his whole world. So he says it. “I’m not going to change who I am, though. I’m not going to stop analysing the world. I’m not going to instantly know your every wish. You still have to tell me things. More than once. More clearly. Maybe even more than you would normally have to with other guys. Because I’m not them.”

His phone is vibrating constantly now, as though a phone call is coming in.

She finally sets her tea down and speaks, “You’re right. This isn’t all your fault, and I can try harder. Your willingness to take the first step is all I really needed.” She pauses for a moment, then shakes her head, “Now answer your phone before you go insane.”

Bill is not sure how he feels. The operation was completely successful, and yet it had been a colossal failure. Every single person they had set out to question had been easily found and questioned. None had evaded them, none of their information had led them to a dead-end. By operational standards, a resounding success.

Not a single person they had questioned, however, had ties to child pornography groups. At least, not so far as they could discern. Not a one. Well, except for the teenagers they had caught with pictures of girlfriends and boyfriends. Hardly the sort of contraband they were after.

What had gone wrong? The FBI had nailed a guy based on their surveillance, how could a large-scale operation here fail to be at least a little successful?

The techies told him it was the nature of the surveillance. People with MusicBox knew it was tapped. They were unlikely to store their pornography on the same machine. Bill knew something, however, that the techies did not know. Data was also being piped in from email and web histories in other ways. Ways which Bill did not himself understand. Then again, some of the techies knew some of these things.

Bill is now looking over the data he has on the successful FBI operation. The one that convinced him that this could work. The pervert had sent an email to his mother, that’s how they had found him. They had picked up the email on his mother’s machine.

Suddenly, Bill has an insight. They had been looking for suspicious material, and tracking that material to the machines it had passed through. Those machine were accessed who knows how, or maybe the wifi connections associated with the machines were compromised, whatever. The success with the FBI had been tracking activity from someone suspicious to a known connection, then back-tracing the activity to the real source somehow. The technical details don’t matter. Bill doesn’t really understand them. The strategy matters. Stop spying only on suspicious data, and start trying to correlate it with otherwise innocent data!

This is great. This can work. He cannot task anyone to do it, however, until the politics gets sorted out. To ask anyone to do this would be to admit that they were not only interested in raw MusicBox data. With the right spin, the operation could still provide the media bomb they need.

He dials his supervisor.

Acklas hangs up as soon as Jack answers. Soon Jack has caught up on the messages they’ve been sending him and begins to reply.

14:00 <acklas> So, I just quit my job.

14:02 <nicnus> Awesome?

14:02 <acklas> nicnus: I’ll let you know. I don’t suppose there are openings at your startup?

14:03 <nicnus> We’re “always hiring”, so maybe.

14:05 <nicnus> Woah! Have you seen this thing about the kiddie porn arrests?

14:06 <acklas> Just a sec…

14:10 <acklas> Woah. That is not how it seemed to be going down at all…

14:12 <nicnus> You mean the part about how this shows there is “more of this problem than we, as a society, want to admit”?

14:12 <acklas> Yeah. From what I understood, it was mostly a junk operation. Scaring old ladies and single mothers, instead of doing their jobs.

14:13 <nicnus> Apparently, they’d rather not remember it that way.

14:15 <acklas> Apparently, they’d rather ask for sweeping surveillance provisions. Because, you know, this proves they can totally handle it.

14:15 <nicnus> No, this proves they might be able to scare people into thinking we need it.

14:15 <acklas> Suresure. Man, I found one article that says the USA “has been doing something similar for some time”

14:16 <nicnus> First I’ve heard of it. I think it’s more that they’re also *trying* to get something in place.

14:16 <acklas> Yeah. The security community would have seen something by now if they were doing it before the laws were in place.

14:20 <nicnus> Jack!

14:20 <acklas> What?

14:20 <nicnus> jjdavis: What, exactly, did you stumble on again? The US gov’t snooping emails?

14:21 <acklas> Woah, when did this happen?

14:21 <nicnus> During your meeting.

14:22 <acklas> jjdavis: is this true?

14:23 <nicnus> jjdavis? This is a big deal, man. You can’t stay down there.

14:24 <acklas> Yeah, if they’re spying semi-illigally…

14:24 <nicnus> If it’s not even legal… yeaah

14:35 <nicnus> jjdavis?

14:40 <nicnus> Oh my goodness, they’ve got him.

14:45 <acklas> dan’t be silly. he’s at his girl’s, remember?

14:50 <acklas> Ok, I’ll call him. You know. He should at least know about this.

** jjdavis reads

14:57 <jjdavis> nicnus: like I said, I’m not going anywhere. I’ll be ok

14:58 <acklas> Sure, I mean, nicnus is a bit paranoid. Still, though…

14:59 <jjdavis> From the way that article reads, it’s about to get hot up there as well.

15:00 <nicnus> maybe, but you didn’t *find* them up here.

15:01 <jjdavis> We’ll see

15:03 <acklas> How did it go with your SO, anyway?

15:05 <jjdavis> I’m still here. We’ve sort of patched things up in principle.

15:06 <acklas> I guess that’s good, then.

15:07 <nicnus> jjdavis: I’m overnighting you one of our devices, set with a key I’ll give to acklas as well.

15:09 <jjdavis> nicnus: sure

15:16 <acklas> nicnus: how hard would it be to modify your MusicBox blocker to instead spam the crap out of the RCMP?

15:18 <nicnus> Pretty easy. I think someone already did it in principle. why?

15:20 <acklas> I’ll get back to you on that.

Acklas is thinking. If the States is already on it, then Canada might not be so far behind. There needs to be resistance to the surveillance before it becomes law. After that, everything just gets harder. Even Nicnus’ magic crypto doesn’t stop spyware. Doesn’t stop email snooping. At least, not by itself.

If they were going to use their failure to tell the media why they needed more power (“they” in this case being some nameless force Acklas paints as being behind the whole mess), then Acklas could maybe use something else to sway the media another way. A battle in the media might be a battle that could be won.

Why has he not thought of this before? Man. Half a day out of the cubicle and already his creative problem-solving juices are flowing again. He has nothing else that he needs to be doing, and money is not going to be an issue for awhile.

If this is what’s necessary to keep from living in fear, then Acklas will bring the fight.