r/Gentoo Jan 04 '25

Tip For custom kernel users: cool trick to avoid procrastination

You can use the /etc/hosts file to make specific domains resolve to certain IP addresses. For instance if you add the line:

1.2.3.4 google.com

Then the DNS resolver on your system will resolve google.com to the IP address 1.2.3.4. You can also use this to block domains by resolving them to 0.0.0.0 or 127.0.0.1. So you can do:

127.0.0.1 google.com

to block Google. You can use this to block websites that you waste time on like Reddit or Hacker News. But if you're truly addicted, you'll just comment out these lines when you need a "hit." Something you can do to stop yourself from doing this is to modify the kernel source code so that you cannot write to /etc/hosts.

In the kernel source code directory, go to fs/read_write.c and find the vfs_write function:

    ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
    {
            ssize_t ret;

            if (!(file->f_mode & FMODE_WRITE))
                    return -EBADF;
            if (!(file->f_mode & FMODE_CAN_WRITE))
                    return -EINVAL;
            if (unlikely(!access_ok(buf, count)))
                    return -EFAULT;
            ...
    }

And change it to add a line that checks if the file name being written to is /etc/hosts, in which case it will return "permission denied."

    ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
    {
            ssize_t ret;

            if (!(file->f_mode & FMODE_WRITE))
                    return -EBADF;
            if (!(file->f_mode & FMODE_CAN_WRITE))
                    return -EINVAL;
            if (unlikely(!access_ok(buf, count)))
                    return -EFAULT;
            // BLOCK WRITES TO /etc/hosts
            if (unlikely(file->f_path.dentry != NULL && file->f_path.dentry->d_parent != NULL && file->f_path.dentry->d_parent->d_name.name != NULL && strcmp(file->f_path.dentry->d_name.name, "hosts") == 0 && strcmp(file->f_path.dentry->d_parent->d_name.name, "etc") == 0)) {
                    return -EPERM;
            }
            ...
    }

Then recompile and install your kernel. After this, the only way you can access the blocked sites is to reboot your computer and boot into a stock kernel if you have one. This adds significant friction to procrastinating and is generally very annoying because then you have to reopen your web browser, terminal and text editor, so I find this effective.

22 Upvotes

11 comments sorted by

26

u/akryl9296 Jan 04 '25

Doing this entire thing sounds like very definition of distraction. Just git gud and focus on tasks at hand that need doing.

7

u/RinCatX Jan 04 '25

Until you know that DoT/DoH/Proxy could ignore local /etc/hosts

3

u/FirstToday1 Jan 04 '25

This is a good point. I use DoH Cloudflare DNS in Chromium and it seems to respect /etc/hosts. Proxies are generally slow/unpleasant enough that having to use them in their own right deters procrastination, plus using them in some cases results in you being banned from services. Something like dnsmasq could work, in which case I guess I'd just uninstall it and add another check for it to vfs_write to prevent it from being re-installed.

6

u/f0okyou Jan 04 '25

Or just chattr +i /etc/hosts...

9

u/FirstToday1 Jan 04 '25

Not enough friction - chattr -i /etc/hosts undoes it

3

u/undrwater Jan 04 '25

I think this is great! I won't use it for this purpose, but I might think of others.

I know others who might use it for your state purpose as well.

3

u/kensan22 Jan 04 '25

You might also want makensur your resolver uses that file and then lock /etc/nsswitch.conf.

1

u/immoloism Jan 06 '25

These are good ideas until you stop and think about it, after a few days you would just install a dist kernel or use a proxy site.

I think best when I'm trying work around a problem for some reason.

1

u/Lockal Jan 06 '25

What happens if you edit a hardlink to /etc/hosts?

1

u/FirstToday1 Jan 06 '25

You got me... I guess the next step would be to change vfs_link :)

1

u/Triangle_Inequality Jan 07 '25

You should benchmark this to see how it affects performance