Skip to content

Quirky fix to Magic Mouse 2 driver not loading when reconnecting - #1

Open
RicardoEPRodrigues wants to merge 40 commits into
rohitpid:magicmouse2from
RicardoEPRodrigues:magicmouse2
Open

Quirky fix to Magic Mouse 2 driver not loading when reconnecting#1
RicardoEPRodrigues wants to merge 40 commits into
rohitpid:magicmouse2from
RicardoEPRodrigues:magicmouse2

Conversation

@RicardoEPRodrigues

Copy link
Copy Markdown

This fix is based on the work of 0xABAD, but I changed his method of detecting the mouse by searching for the Product ID and not depend on the unit codes.

This fix is based on the work of [0xABAD](https://github.com/0xABAD/magic-mouse-2), but I changed his method of detecting the mouse by searching for the Product ID and not depend on the unit codes.
README now has information on the new script and on how the fixes work.
When an USB with that product ID is detected then a input driver is loaded. I think this is the correct interpretation.
Previously the rule used the USB ID of the computer and not the mouse.
@n0ni0

n0ni0 commented Feb 18, 2020

Copy link
Copy Markdown

hello @RicardoEPRodrigues and thx for your effort to solve this problem.

I have applied your changes and the mouse has stopped working.
It's connected but does not move.

I tried removing the files and reinstalling but still not moving in when I run install.sh

Running in ubuntu 18.04 with kernel 5.3.0-28-generic
Blueman-manager 2.0.5

any suggestions?

thx

@RicardoEPRodrigues

Copy link
Copy Markdown
Author

Hey @n0ni0, can you check if the logs have anything relating to udev or bluetooth?

If the mouse stopped working then part of the script is running. We have to find out if any error came up and what that error is. Can you check if the /opt/magic-mouse-fix/hid-magicmouse.ko file exists?

To revert the changes you can simply delete the udev rule file (sudo rm /etc/udev/rules.d/10-magicmouse.rules) and reboot. Have you tried rebooting after installing the driver?

@n0ni0

n0ni0 commented Feb 18, 2020

Copy link
Copy Markdown

I found a log file when connect device in local/share/xorg/Xorg.0.log:

91.663] (**) Antonio Jiménez Mouse: Applying InputClass "libinput pointer catchall"
91.663] (II) Using input driver 'libinput' for 'Antonio Jiménez Mouse'
91.697] (EE) systemd-logind: failed to take device /dev/input/event16: No such device
91.697] (**) Antonio Jiménez Mouse: always reports core events
91.697] (**) Option "Device" "/dev/input/event16"
91.697] (**) Option "_source" "server/udev"
91.697] (EE) client bug: Invalid path /dev/input/event16
91.697] (EE) libinput: Antonio Jiménez Mouse: Failed to create a device for /dev/input/event16
91.697] (EE) PreInit returned 2 for "Antonio Jiménez Mouse"
91.697] (II) UnloadModule: "libinput"
91.851] (II) config/udev: Adding input device (unnamed) (/dev/input/mouse2)
91.851] (II) No input driver specified, ignoring this device.
91.851] (II) This device may have been added with another device file.

The file /opt/magic-mouse-fix/hid-magicmouse.ko exist

After restart the problem persist.

@RicardoEPRodrigues

Copy link
Copy Markdown
Author

I currently don't have the time to look into what the issue is.

If you can please do some testing. First off delete the udev rule file (sudo rm /etc/udev/rules.d/10-magicmouse.rules) and restart udev (udevadm control -R), this will disable the script from being loaded when you connect the mouse. Check if the mouse works again without scroll.

After that you can try running the script yourself with sudo and see if the new driver is loaded: sudo /opt/magic-mouse-fix/magic-mouse-2-add.sh. Is the mouse moving? Is the scroll working?

Explore a bit of what the script does. These are its contents:

#!/bin/sh

reload() {
    modprobe -r hid_magicmouse
    insmod /opt/magic-mouse-fix/hid-magicmouse.ko \
           scroll_acceleration=1 \
           scroll_speed=25 \
           middle_click_3finger=1
}

reload &

As you can see it removes any magic mouse driver (hid_magicmouse) and adds our driver with some tweaks. Try removing the tweaks and see what you get.

I'll try to help more when I have time.

@n0ni0

n0ni0 commented Feb 19, 2020

Copy link
Copy Markdown
  • Deleting the udev file and restarting udev, the mouse continue working with scroll, when restart scroll not work.

  • Running sudo /opt/magic-mouse-fix/magic-mouse-2-add.sh the mouse works with scroll without any problems, but after restart computer scroll not work.

  • Launching ./install.sh continue with the same problems of move and scroll.

I will try to investigate more, thx again.

Also added a smaller install script that does not install dkms and does not install the bluetooth fix.
@RicardoEPRodrigues

Copy link
Copy Markdown
Author

@n0ni0 I updated my branch, can you test it out?

Apparently the UDEV rule will always try to run at least 3 times (loading and unloading the driver each time). I tried to limit the number of repetitions, but could only lower it to 3. To solve this I modified the script to check if others were running. So now I believe it will always run only one time!

You can use a new smaller script sudo ./install-fix.sh that will try to only run what is needed.

Added a delay after removing the previous driver and before adding the new one
@n0ni0

n0ni0 commented May 25, 2020

Copy link
Copy Markdown

Hello @RicardoEPRodrigues, using install-fix.sh works like a charm.

Thanks

@jaybeaton

Copy link
Copy Markdown

@RicardoEPRodrigues

This is working for me to allow scrolling each time my mouse connects (thanks!), but the middle-click seems to have stopped working. It had been working fine with the @rohitpid version. Any idea what might be going on or how to fix this?

@RicardoEPRodrigues

RicardoEPRodrigues commented Jun 14, 2020

Copy link
Copy Markdown
Author

@jaybeaton Great to know it is working!

On theory, the middle-click is on by default and should work. You can tweak the settings in the magic-mouse-2-add.sh. After tweaking the values you need to run sudo ./install-fix.sh

Unfortunately, I haven't tested it myself.

@cavaughankirov

Copy link
Copy Markdown

So it looks like I have a similar issue. On any reboot I have to I have to reapply the Troubleshooting fix to get my MM2 to scroll. In the aforementioned posts you talk about some install-fix.sh script. But where is it? Or is there another solution?
Thanks!

@RicardoEPRodrigues

Copy link
Copy Markdown
Author

@cavaughankirov this is a pull request with some changes. If you wanna try them out you have to download the changes in my repo: https://github.com/RicardoEPRodrigues/Linux-Magic-Trackpad-2-Driver

Just run install.sh and it should fix your issues. Let me know if it works for you!

@cavaughankirov

cavaughankirov commented Jul 20, 2020

Copy link
Copy Markdown

well, there's something wrong with the script and I wish I new coding better to try and figure it out. I moved the script to the scripts dir. But when running it it seems to reproduce the scripts dir. Here's the output:
``
~/Apps/MMouse/Linux-Magic-Trackpad-2-Driver/scripts$ sudo ./install.sh
+++ dirname ./install.sh
++ cd .
++ pwd

  • DIR=/home/me/Apps/MMouse/Linux-Magic-Trackpad-2-Driver/scripts
  • OPT=/opt/magic-mouse-fix
  • UDEV=/etc/udev/rules.d
  • chmod u+x /home/me/Apps/MMouse/Linux-Magic-Trackpad-2-Driver/scripts/scripts/install.sh
  • /home/me/Apps/MMouse/Linux-Magic-Trackpad-2-Driver/scripts/scripts/install.sh
    +++ dirname /home/me/Apps/MMouse/Linux-Magic-Trackpad-2-Driver/scripts/scripts/install.sh
    ++ cd /home/me/Apps/MMouse/Linux-Magic-Trackpad-2-Driver/scripts/scripts
    ++ pwd
  • DIR=/home/me/Apps/MMouse/Linux-Magic-Trackpad-2-Driver/scripts/scripts
  • OPT=/opt/magic-mouse-fix
  • UDEV=/etc/udev/rules.d
  • chmod u+x /home/me/Apps/MMouse/Linux-Magic-Trackpad-2-Driver/scripts/scripts/scripts/install.sh
    chmod: cannot access '/home/me/Apps/MMouse/Linux-Magic-Trackpad-2-Driver/scripts/scripts/scripts/install.sh': No such file or directory

@RicardoEPRodrigues

Copy link
Copy Markdown
Author

@cavaughankirov you don't need to move the script anywhere. Just use my repo without modification and in the base directory run sudo ./install.sh.

RicardoEPRodrigues and others added 7 commits July 21, 2020 20:07
for newer systems bluetooth restart now is done with systemctl.

It also adds a small check to detect root. If not it asks to run it as sudo.
This adds a 1/5 of a second delay from the registration of a drag event
to actually starting to scroll. The result is a slight decrease in
sensitivity when dragging a finger on the Magic Mouse to start a scroll.
The actual scrolling is the same as before, there is a just a small
delay at the beginning of the stroke which helps to eliminate accidental
scrolls when just resting the finger lightly on the mouse surface.
This update simplifies the code. Please run `remove.sh` before updating the driver to ensure a smooth transition.

Removed unnecessary files from repo.

Changes were made to install, the main loading script to make use of modprobe more accurately.

The parameters for hid-magicmouse were moved to /etc/modprobe.d/hid-magicmouse.conf . This allows the unloading and loading of the module with predefined parameters.

A `remove.sh` script was added to uninstall the driver. NOTE: It does not remove the bluetooth fix.

Changed DKMS version to reflect the driver name.
Also add parameter definition to conf file.
@sirjoshua

Copy link
Copy Markdown

This fix works great for my magic mouse 2 on Linux Mint (Ubuntu 20.04) kernel 5.4.0-67-generic. I was having trouble with getting the rohitpid/magicmouse2 driver to work (I don't think it was even installing/loading) so i ran the remove script and tried this PR instead.

The install went smooth as butter and the scroll is now working perfectly - thank you 👍 😄

RicardoEPRodrigues and others added 12 commits June 7, 2021 18:48
This fix removes the need for external files to load the driver. It turns out the driver already loaded itself, but without a delay it would fail. This is now fixed.

Based on the work seen here johnchen902/linux@cb700d0
This update brings a feature that allows you to stop scrolling if the mouse is moving. It is still very rough, but it works.
Add reload driver instructions to README file
…RT_ID

Backports upstream commit d93ba918a185 ("HID: magicmouse: Prevent
out-of-bounds (OOB) read during DOUBLE_REPORT_ID") by Lee Jones.

When a 0xf7 DOUBLE_REPORT_ID packet is received, the driver recursively
calls magicmouse_raw_event with `data + 2` and `data[1]` as the new size.
Without validation, two issues arise:

  - If size < 2, reading data[1] is out-of-bounds.
  - If data[1] > size - 2, the first recursive call thinks it has more
    bytes than were actually received, and the recursive parse reads
    past the actual report buffer.

This mirrors a class of bug seen on other HID parsers (cf.
CVE-2026-43140 on the same driver) where a fake/malicious USB or
Bluetooth device controls report length independently of declared size.

The fix is the same one already in mainline since v6.16-rc1: add an
early size>=1 guard at function entry, and validate size>=2 and
data[1]+2<=size before recursing.

Reference:
  https://git.kernel.org/linus/d93ba918a185aca2594da63e92fdc5495b559c0f

Reported-by: Aleksandr Kovalko <gistrec@gmail.com>
The current size check for MOUSE2_REPORT_ID is inverted: it only
returns when `size > 14 && (size - 14) % 8 != 0`, which lets any
`size <= 14` fall through.

For reports of size 1..5, this fallthrough then executes:

    x = (int)((data[3] << 24) | (data[2] << 16)) >> 16;
    y = (int)((data[5] << 24) | (data[4] << 16)) >> 16;
    ...
    clicks = data[1];

These read past the end of the actually-received HID buffer.  The
resulting x and y get emitted to userspace as REL_X / REL_Y mouse
motion events, so a malicious or buggy USB/Bluetooth HID device
pretending to be Apple Magic Mouse 2 can leak a small number of
bytes of adjacent kernel memory to userspace mouse listeners.

A userspace ASan repro of the parser path confirms the OOB read:

    AddressSanitizer: heap-buffer-overflow ...
    READ of size 1 at 0x6020000000b3 ... 2 bytes after 1-byte region
    #0 magicmouse_raw_event ... (in `x = ((data[3] << 24) | ...`)

Mainline drivers/hid/hid-magicmouse.c has the correct check since
the Magic Mouse 2 support patch landed there in 2021 (commit
2b0c086cd6f4, "HID: magicmouse: add Apple Magic Mouse 2 support"):

    if (size != 8 && (size < 14 || (size - 14) % 8 != 0))
        return 0;

This accepts the documented MM2 sizes (8 for the no-touch report,
or 14 + 8*N for N-finger reports) and rejects everything else.

Bring this fork in line with mainline.

Reported-by: Aleksandr Kovalko <gistrec@gmail.com>
hid-magicmouse: backport upstream OOB read fix for DOUBLE_REPORT_ID
hid-magicmouse: fix MOUSE2_REPORT_ID size check (inverted condition)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants