Regression with Gradient Descent in Low-level Matlab
June 25, 2010
I just finished writing my first machine learning algorithm in Matlab. The algorithm is based on gradient descent search for estimating parameters of linear regression (but can be easily extended to quadratic or even higher-dimensional polynomials). It’s fairly easy if you know the theory behind the model. So, first a very brief theory portion. This isn’t a tutorial on statistics. Go read a book if you don’t know about regression.
Writing a Skeleton Linux Security Module
May 23, 2010
I recently had to write a Linux Security Module (LSM) for one of my research projects and I was surprised to find that there are few tutorials out there and most of them don’t work because of the discrepencies in kernel versions. They’re talking about 2.000.x kernel versions! The only good tutorial I came across was the one on Linux Journal but it assumed some background knowledge (which I didn’t have). So, I had to struggle for a day to figure out how to go about writing the LSM. Hence, this tutorial.
Now, this isn’t strictly a newbie tutorial. It isn’t even a tutorial per se. It’s more like a guideline that will tell you where to look to find what you need. The reason is that if I write something specific, it’ll go out-of-date in a giffy. So, I’ll tell you the process of how I figured out the steps and you can (probably) reproduce them even if the specifics have changed. This might not be the best way to do it but it certainly gets you going. I had to take quite a few detours to get to these steps; so they might save you some time. Ok, let’s go.
Read the rest of this entry »
Writing a Minimal Eclipse Plugin
April 15, 2010
I have tried, on multiple occasions, to write an eclipse plugin. I always gave up in the middle of the process though, what with the different versions of eclipse floating around and the discrepancies between the tutorials and the version I was using. I finally decided to sit down and go through a simple ‘hello world’ plugin and write a tutorial in the process to guide those that are using the latest eclipse. This is a tutorial for eclipse 3.5 (galileo) and is a no BS tutorial with little or no explanation. That’s because I believe in code first, think later approach when learning new technologies. So, download eclipse SDK version (not the Java or the C/C++ version) and read on.
Create Ubuntu Live USB in Windows
January 29, 2010
If you’ve ever had trouble installing Ubuntu because you couldn’t use a CD (because of a bad CD, a thoroughly malicious drive or, as in my case, a small-form CD stuck in your slot-load mechanism), you might want to read this. This mini-tutorial (slash how-to) is going to show you a clean and concise, no-BS mechanism for installing Ubuntu 8.04 Hardy Heron without the need for a CD or a working Linux installation. I didn’t discover this method. Just came across it somewhere and can’t find the link in Google. So, here it goes. (I’m assuming your USB is F: drive)
- Get syslinux (this will allow you to make your USB active and install a bootloader on it — I used version 3.81).
- Format your (700M+) USB drive with a FAT32 filesystem.
- Extract syslinux, go to the win32 folder in the extracted files and execute (from ‘cmd’): syslinux.exe -ma f: … ‘m’ installs the bootloader and ‘a’ makes it active.
- Get Ubuntu 8.04 iso image and use an un-archiver to extract it. Winrar works for me.
- Copy all extracted files to f:
- Copy f:/isolinux/* to f:/*
- Rename isolinux.cfg to syslinux.cfg
- Reboot and boot from your USB drive (you may need to change BIOS settings for this).
Voila! You have Ubuntu working.
Note: Ubuntu 9.10 didn’t work for me. I got to the X part but only a “working” cursor showed with no progress. So, let me know if you get it working with this method.
Matlab 7 under Windows 7
July 30, 2009
I had trouble getting Matlab 7 to work with Windows 7 RC even after setting the compatibility options. So, after a bit of search, here’s the solution (copied from ELFEHRIJ on http://bit.ly/Jt7ij with some minor changes).
After installing Matlab (use Classic Windows Theme if you have problems with the installer):
- To use Matlab with other windows 7 themes you have to change Java VM used by matlab.
- Download latest Java version and install it.
- Go to <Matlab-installation-dir>\sys\java\jre\win32 you will find a folder named jre1.5.0_07 (or a similar version). Rename it to Original_<whatever was originally there>
- Go to C:\Program Files\Java you will find file named jre6. Copy it to <Matlab-installation-dir>\sys\java\jre\win32 and rename it to <original jre directory name>
This will ensure that Matlab uses the new JRE instead of the old one. Just start Matlab now!
Update: Thanks to “ra” and larry for the corrections.
Flashing Android Dev Phone 1
July 20, 2009
This tutorial is about flashing your Android Developer Phone 1 with your own custom build. It will provide a concise description of steps involved along with a special portion on how to port Google’s apps on your custom build. I found that particularly troublesome with little help on the Internet. So, that will be a bonus
First the disclaimer: This is for your Android Dev Phone 1 (ADP1). If you’re using T-Mobile’s SIM/firmware locked phone, stop. This tutorial is not for you. If you’re using ADP1, proceed at your own risk. You may brick your phone if you do something wrong and I shall not be held responsible for it. Finally, you might want to backup your factory-provided image. I don’t think it’s really necessary because you can just flash it again using the HTC provided images.
So, here is how it’s done:
Getting Started with Android Dev Phone 1
July 16, 2009
We received our Google Android Dev Phone 1 yesterday and immediately ran into trouble. We don’t have a supported carrier here and we couldn’t get our own carriers to work with Android because we didn’t have the APN information. Android’s distro that comes bundled into the Dev Phone won’t let you in without an APN though. You get a “SIM not found” message and you can’t do anything other than dial an emergency number. So, after searching for a while, I found some useful tips for getting around the problem.
First, you need to plug in your phone through the provided USB. If you’re running XP, the device will probably not be recognized. (It wasn’t for me.) So, download the Android phone driver here (or here) and install it when XP asks to search for a driver. (Thanks to anddev for this information.) After that, get the Android SDK from here. Go to command prompt and navigate to the tools directory in the SDK. Then execute these commands.
adb shell
su
cd /data/data/com.android.providers.settings/databases
sqlite3 settings.db
INSERT INTO system (name, value) VALUES ('device_provisioned', 1);
.exit
reboot
Once the device finishes rebooting,
adb shell
am start -a android.intent.action.MAIN -n com.android.settings/.Settings
Many thanks to Android Tricks for writing this tip.
Update 1: Android SDK ships with the latest version of the windows Android phone driver. You can find it in $ANDROID_SDK_HOME/usb_driver. So, you don’t need to download the driver using the links provided above.
Update 2: To get the Android device to work on Ubuntu 9.04 Jaunty Jackalope, you need to perform the following steps:
sudo nano /etc/udev/rules.d/51-android.rules- Add this line to the file:
SUBSYSTEM=="usb", ATTRS{idVendor}=="0bb4", MODE="0666"
(You can get the 0bb4 value from lsusb for High Tech Corporation (i.e. HTC) if you work with a different phone) sudo chmod a+rx /etc/udev/rules.d/51-android.rulessudo /etc/init.d/udev restartadb devices(to see the device)
Getting started with Openmoko
September 7, 2008
For record purposes, here’s what I did to get Openmoko working.
- Installed Ubuntu. (Had many problems with Fedora. Don’t know if it was because of the distribution though.)
- Installed all the dependencies and got the MokoMakeFile (see here).
- sudo su and echo 0 > /proc/sys/vm/mmap_min_addr
- make openmoko-setup
- make setup-machine-freerunner
- make openmoko-devel-image
- Backed up the moko folder
Writing a basic SELinux LPM
August 27, 2008
I couldn’t find a straight forward tutorial on how to create an LPM in SELinux. So, here goes. Notice that this is for educational purposes only and would only take you so far. You need to study the implications of LPMs and how SELinux works before you can actually use them.
Assumptions:
- You know what SELinux is.
- You know what types, attributes, macros etc are in SELinux.
- You have read about Loadable Policy Modules (LPMs) and want to use them.
- You don’t know how to write and compile/use an LPM.
Setup:
- Set SELinux to permissive. (Not enforcing. This isn’t a complete tutorial!)
- Get setools package (possibly from yum)
- Install selinux-policy-devel package (possibly from yum)
Operations: (su whenever you need to)
- Create a directory. I’m assuming /home/user/lpm
- cd into the directory and create a sample policy. (See below for policy where it will be explained). Let’s call it myapp.te
- Copy the LPM makefile from /usr/share/selinux/devel. (This comes with selinux-policy-devel)
- Run “make” < compiles the .te file
- Run “semodule -i myapp.pp”< loads the LPM
- Run “cp /usr/bin/tail ./tt2″ < copies the tail script here for testing purposes
- Run “touch myapplog.log” < create a dummy log file
- Run “chcon -t myapp_exec_t tt2″ < Assign label to executable (see policy for description)
- Run “chcon -t myapp_log_t myapplog.log” < Assign label to log file
- Run “seaudit”. Open log file “/var/log/messages” if you do not have auditd installed or “/var/log/audit/audit.log” if you do.
- Run “./tt2 myapplog.log” < Run the file.
- Take a look at the seaudit for auditting details. (See attached screenshots for details)
- Notice the denies. (tail requires many allows which we haven’t put in the LPM. You can continue to fix these to get rid of all the denies.)
The policy:
-----------------------| myapp.te |---------------------
policy_module(myapp,1.0.0)
# Adopted from /usr/share/selinux/devel/example.te
# Declarations
require{
type fs_t;
type unconfined_devpts_t;
}
type myapp_t;
type myapp_exec_t;
domain_type(myapp_t)
# myapp_t is a process (belongs to domain attribute)
domain_entry_file(myapp_t, myapp_exec_t)
# allow myapp_t to be transitioned to using myapp_exec_t
type myapp_log_t;
logging_log_file(myapp_log_t)
type myapp_tmp_t;
files_tmp_file(myapp_tmp_t)
domain_auto_trans(domain, myapp_exec_t, myapp_t)
# transition from 'domain' (any process) to myapp_t through myapp_exec_t executable
#
# Myapp local policy
#
allow myapp_exec_t fs_t:filesystem associate;
allow myapp_t unconfined_devpts_t : chr_file *;
# allow myapp_t access to the terminal
allow domain myapp_exec_t : file {execute read write} ;
# allow any process to run myapp_exec_t
auditallow myapp_t myapp_log_t:file ra_file_perms;
auditallow myapp_t myapp_log_t:file read;
# auditallow myapp_t to read myapp_log_t
allow myapp_t myapp_tmp_t:file manage_file_perms;
files_tmp_filetrans(myapp_t,myapp_tmp_t,file)
-----------------------------------------------------------
Screencaps:
- SETroubleshoot showing error message (denied access to terminal) [see here]
- SEAudit showing log with denies and grants [see here]
audit2allow:(updated)
If you want to generate allow rules automatically, use audit2allow:
audit2allow -i /var/log/audit/audit.log
This will show you the rules which need to be added to the .te file for the whole thing to work.
XPath Example
March 5, 2008
A simple XPath example using JAXP only:
package serg.xslt;import java.io.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.InputSource;
import com.sun.org.apache.xpath.internal.CachedXPathAPI;
public class PathSelector {
public static void main(String arg[]) throws Exception {
String filename = null;
String xpath = null;
filename = arg[0];
// xpath = arg[1];
xpath = "//book/following-sibling::*";
// set up a dom tree
InputSource in = new InputSource(new FileInputStream(filename));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = dbf.newDocumentBuilder().parse(in);
System.out.println("Querying Dom using : " + xpath);
CachedXPathAPI path = new CachedXPathAPI();
NodeIterator nl = path.selectNodeIterator(doc, xpath);
// the actual XPath selector
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
System.out.println("");
Node n;
while ((n = nl.nextNode()) != null) {
trans.transform(new DOMSource(n), new StreamResult(
new OutputStreamWriter(System.out)));
}
System.out.println("");
}
}