mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ubisectech Sirius" <bugreport@valiantsec.com>
To: "linux-kernel" <linux-kernel@vger.kernel.org>
Cc: "peterz" <peterz@infradead.org>, "mingo" <mingo@redhat.com>,
	"acme" <acme@kernel.org>, "namhyung" <namhyung@kernel.org>
Subject: WARNING in intel_pmu_lbr_counters_reorder
Date: Mon, 30 Sep 2024 15:46:44 +0800	[thread overview]
Message-ID: <fdbb2d32-157f-4879-ab1c-a6f10f1b3286.bugreport@valiantsec.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2865 bytes --]


Hello.
We are Ubisectech Sirius Team, the vulnerability lab of China ValiantSec. Recently, our team has discovered a issue in Linux kernel 6.11.0-rc2-g6a0e38264012. Attached to the email were a PoC file of the issue.

Stack dump:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 13222 at arch/x86/events/intel/lbr.c:957 intel_pmu_lbr_counters_reorder.isra.0+0x61f/0x750 arch/x86/events/intel/lbr.c:957
Modules linked in:
CPU: 0 UID: 0 PID: 13222 Comm: syz.3.335 Not tainted 6.11.0-rc2-g6a0e38264012 #49
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
RIP: 0010:intel_pmu_lbr_counters_reorder.isra.0+0x61f/0x750 arch/x86/events/intel/lbr.c:957
Code: 00 48 8d bb 20 01 00 00 48 89 f8 48 c1 e8 03 42 80 3c 20 00 0f 84 26 ff ff ff e8 ac 96 df 00 e9 1c ff ff ff e8 22 cd 83 00 90 <0f> 0b 90 e9 80 fb ff ff 48 c7 c7 60 87 6c 8d e8 0d c0 8e 03 e9 79
RSP: 0000:ffffc90001d77910 EFLAGS: 00010046
RAX: 0000000080110000 RBX: ffff888000155070 RCX: ffffffff8105486e
RDX: ffff888043692500 RSI: ffffffff81054cee RDI: 0000000000000005
RBP: ffff888000155070 R08: 0000000000000005 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000000 R12: dffffc0000000000
R13: 0000000000000000 R14: ffff88802c622bb8 R15: 0000000000000000
FS:  00007f3f01bc8640(0000) GS:ffff88802c600000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 000000002001d000 CR3: 000000005c572000 CR4: 0000000000750ef0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000600
PKRU: 55555554
Call Trace:
 <TASK>
 intel_pmu_lbr_save_brstack+0x1ef/0x3b0 arch/x86/events/intel/lbr.c:976
 handle_pmi_common+0x744/0xbd0 arch/x86/events/intel/core.c:3118
 intel_pmu_handle_irq+0x2b7/0x10b0 arch/x86/events/intel/core.c:3181
 perf_event_nmi_handler arch/x86/events/core.c:1747 [inline]
 perf_event_nmi_handler+0x4c/0x70 arch/x86/events/core.c:1733
 nmi_handle+0x1a6/0x5b0 arch/x86/kernel/nmi.c:151
 default_do_nmi+0x88/0x220 arch/x86/kernel/nmi.c:351
 exc_nmi+0x262/0x310 arch/x86/kernel/nmi.c:545
 asm_exc_nmi+0xb8/0x101 arch/x86/entry/entry_64.S:1192
RIP: 0033:0x7f3f00c4a51a
Code: 00 00 00 ff 89 81 90 00 00 00 75 04 85 c0 74 1c 48 63 44 24 18 48 8d 14 80 48 8d 14 50 48 8d 05 6c 16 25 00 48 83 7c d0 28 00 <74> 0f 48 8b 44 24 08 c7 80 90 00 00 00 16 00 00 00 48 8b 54 24 08
RSP: 002b:00007f3f01bc7fc0 EFLAGS: 00000246
RAX: 00007f3f00e9bb80 RBX: 00007f3f00f35f80 RCX: 00007f3f00f35f80
RDX: 0000000000007a81 RSI: 0000000000000000 RDI: 0000000020000300
RBP: 00007f3f00e1bd8a R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000005 R11: 0000000000000246 R12: 0000000000000000
R13: 0000000000000000 R14: 00007f3f00f35f80 R15: 00007f3f01ba8000
 </TASK>

Thank you for taking the time to read this email and we look forward to working with you further.





[-- Attachment #2: poc.c --]
[-- Type: application/octet-stream, Size: 9812 bytes --]

// autogenerated by syzkaller (https://github.com/google/syzkaller)

#define _GNU_SOURCE

#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

static void sleep_ms(uint64_t ms)
{
  usleep(ms * 1000);
}

static uint64_t current_time_ms(void)
{
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts))
    exit(1);
  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}

#define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
#define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len)               \
  *(type*)(addr) =                                                             \
      htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) |           \
            (((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))

static bool write_file(const char* file, const char* what, ...)
{
  char buf[1024];
  va_list args;
  va_start(args, what);
  vsnprintf(buf, sizeof(buf), what, args);
  va_end(args);
  buf[sizeof(buf) - 1] = 0;
  int len = strlen(buf);
  int fd = open(file, O_WRONLY | O_CLOEXEC);
  if (fd == -1)
    return false;
  if (write(fd, buf, len) != len) {
    int err = errno;
    close(fd);
    errno = err;
    return false;
  }
  close(fd);
  return true;
}

static void kill_and_wait(int pid, int* status)
{
  kill(-pid, SIGKILL);
  kill(pid, SIGKILL);
  for (int i = 0; i < 100; i++) {
    if (waitpid(-1, status, WNOHANG | __WALL) == pid)
      return;
    usleep(1000);
  }
  DIR* dir = opendir("/sys/fs/fuse/connections");
  if (dir) {
    for (;;) {
      struct dirent* ent = readdir(dir);
      if (!ent)
        break;
      if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
        continue;
      char abort[300];
      snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
               ent->d_name);
      int fd = open(abort, O_WRONLY);
      if (fd == -1) {
        continue;
      }
      if (write(fd, abort, 1) < 0) {
      }
      close(fd);
    }
    closedir(dir);
  } else {
  }
  while (waitpid(-1, status, __WALL) != pid) {
  }
}

static void setup_test()
{
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  setpgrp();
  write_file("/proc/self/oom_score_adj", "1000");
}

static void execute_one(void);

#define WAIT_FLAGS __WALL

static void loop(void)
{
  int iter = 0;
  for (;; iter++) {
    int pid = fork();
    if (pid < 0)
      exit(1);
    if (pid == 0) {
      setup_test();
      execute_one();
      exit(0);
    }
    int status = 0;
    uint64_t start = current_time_ms();
    for (;;) {
      sleep_ms(10);
      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
        break;
      if (current_time_ms() - start < 5000)
        continue;
      kill_and_wait(pid, &status);
      break;
    }
  }
}

uint64_t r[1] = {0xffffffffffffffff};

void execute_one(void)
{
  intptr_t res = 0;
  if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
  }
  *(uint32_t*)0x2001d000 = 1;
  *(uint32_t*)0x2001d004 = 0x80;
  *(uint8_t*)0x2001d008 = 1;
  *(uint8_t*)0x2001d009 = 0;
  *(uint8_t*)0x2001d00a = 0;
  *(uint8_t*)0x2001d00b = 0;
  *(uint32_t*)0x2001d00c = 0;
  *(uint64_t*)0x2001d010 = 0x41c1;
  *(uint64_t*)0x2001d018 = 0;
  *(uint64_t*)0x2001d020 = 0;
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 0, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 1, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 2, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 3, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 4, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 1, 5, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 6, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 7, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 8, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 9, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 10, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 11, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 12, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 13, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 14, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 15, 2);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 17, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 18, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 19, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 20, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 21, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 22, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 23, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 24, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 25, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 26, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 27, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 28, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 29, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 30, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 31, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 32, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 33, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0x2000, 34, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 35, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 36, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 37, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2001d028, 0, 38, 26);
  *(uint32_t*)0x2001d030 = 0;
  *(uint32_t*)0x2001d034 = 0;
  *(uint64_t*)0x2001d038 = 0;
  *(uint64_t*)0x2001d040 = 0;
  *(uint64_t*)0x2001d048 = 0;
  *(uint64_t*)0x2001d050 = 0;
  *(uint32_t*)0x2001d058 = 0;
  *(uint32_t*)0x2001d05c = 0;
  *(uint64_t*)0x2001d060 = 0;
  *(uint32_t*)0x2001d068 = 0;
  *(uint16_t*)0x2001d06c = 0;
  *(uint16_t*)0x2001d06e = 0;
  *(uint32_t*)0x2001d070 = 0;
  *(uint32_t*)0x2001d074 = 0;
  *(uint64_t*)0x2001d078 = 0;
  res = syscall(__NR_perf_event_open, /*attr=*/0x2001d000ul, /*pid=*/0,
                /*cpu=*/-1, /*group=*/-1, /*flags=*/0ul);
  if (res != -1)
    r[0] = res;
  *(uint32_t*)0x20000300 = 4;
  *(uint32_t*)0x20000304 = 0x80;
  *(uint8_t*)0x20000308 = 0x43;
  *(uint8_t*)0x20000309 = 0x81;
  *(uint8_t*)0x2000030a = 0;
  *(uint8_t*)0x2000030b = 0;
  *(uint32_t*)0x2000030c = 0;
  *(uint64_t*)0x20000310 = 6;
  *(uint64_t*)0x20000318 = 0xc1b06;
  *(uint64_t*)0x20000320 = 0xe;
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 0, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 1, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 2, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 3, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 4, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0x10000003, 5, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 6, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 7, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 8, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 9, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 10, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 11, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 12, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 13, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 14, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 15, 2);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 17, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 18, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 19, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 20, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 21, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 22, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 23, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 24, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 25, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 26, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 27, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 28, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 29, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 30, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 31, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 32, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 33, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 34, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 35, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 36, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 37, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000328, 0, 38, 26);
  *(uint32_t*)0x20000330 = 0x400000;
  *(uint32_t*)0x20000334 = 4;
  *(uint64_t*)0x20000338 = 0;
  *(uint64_t*)0x20000340 = 9;
  *(uint64_t*)0x20000348 = 0x10062;
  *(uint64_t*)0x20000350 = 8;
  *(uint32_t*)0x20000358 = 0xd;
  *(uint32_t*)0x2000035c = 0;
  *(uint64_t*)0x20000360 = 9;
  *(uint32_t*)0x20000368 = 5;
  *(uint16_t*)0x2000036c = 1;
  *(uint16_t*)0x2000036e = 0;
  *(uint32_t*)0x20000370 = 0;
  *(uint32_t*)0x20000374 = 0;
  *(uint64_t*)0x20000378 = 5;
  syscall(__NR_perf_event_open, /*attr=*/0x20000300ul, /*pid=*/0, /*cpu=*/-1,
          /*group=*/r[0], /*flags=*/0ul);
}
int main(void)
{
  syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul,
          /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  const char* reason;
  (void)reason;
  loop();
  return 0;
}

             reply	other threads:[~2024-09-30  7:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-30  7:46 Ubisectech Sirius [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-03-17 12:40 cheung wall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fdbb2d32-157f-4879-ab1c-a6f10f1b3286.bugreport@valiantsec.com \
    --to=bugreport@valiantsec.com \
    --cc=acme@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®