Code Monkey home page Code Monkey logo

linux-hardening's People

Contributors

gustavoarsilva avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

linux-hardening's Issues

Use ARRAY_SIZE()

If you send a proper patch for this CC me: Gustavo A. R. Silva <[email protected]>

linux-next-20220104$ time make coccicheck -j 128 COCCI=scripts/coccinelle/misc/array_size.cocci MODE=patch 2>&1 | tee array_size.patch

Please check for false positives in the output before submitting a patch.
When using "patch" mode, carefully review the patch before submitting it.

/usr/local/bin/spatch -D patch --very-quiet --cocci-file scripts/coccinelle/misc/array_size.cocci --no-includes --include-headers --dir . -I ./arch/x86/include -I ./arch/x86/include/generated -I ./include -I ./arch/x86/include/uapi -I ./arch/x86/include/generated/uapi -I ./include/uapi -I ./include/generated/uapi --include ./include/linux/compiler-version.h --include ./include/linux/kconfig.h --jobs 64 --chunksize 1
6041 files match
diff -u -p a/tools/perf/arch/x86/util/dwarf-regs.c b/tools/perf/arch/x86/util/dwarf-regs.c
--- a/tools/perf/arch/x86/util/dwarf-regs.c
+++ b/tools/perf/arch/x86/util/dwarf-regs.c
@@ -89,7 +89,7 @@ static const struct pt_regs_offset x86_6
 #endif
 
 /* Minus 1 for the ending REG_OFFSET_END */
-#define ARCH_MAX_REGS ((sizeof(regoffset_table) / sizeof(regoffset_table[0])) - 1)
+#define ARCH_MAX_REGS (ARRAY_SIZE(regoffset_table) - 1)
 
 /* Return architecture dependent register string (for kprobe-tracer) */
 const char *get_arch_regstr(unsigned int n)
diff -u -p a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c
--- a/tools/lib/bpf/xsk.c
+++ b/tools/lib/bpf/xsk.c
@@ -481,8 +481,8 @@ static int xsk_load_xdp_prog(struct xsk_
 		BPF_EMIT_CALL(BPF_FUNC_redirect_map),
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt[] = {sizeof(prog) / sizeof(struct bpf_insn),
-			      sizeof(prog_redirect_flags) / sizeof(struct bpf_insn),
+	size_t insns_cnt[] = {ARRAY_SIZE(prog),
+			      ARRAY_SIZE(prog_redirect_flags),
 	};
 	struct bpf_insn *progs[] = {prog, prog_redirect_flags};
 	enum xsk_prog option = get_xsk_prog();
diff -u -p a/arch/powerpc/xmon/ppc-opc.c b/arch/powerpc/xmon/ppc-opc.c
--- a/arch/powerpc/xmon/ppc-opc.c
+++ b/arch/powerpc/xmon/ppc-opc.c
@@ -954,8 +954,7 @@ const struct powerpc_operand powerpc_ope
   { 0xff, 11, NULL, NULL, PPC_OPERAND_SIGNOPT },
 };
 
-const unsigned int num_powerpc_operands = (sizeof (powerpc_operands)
-					   / sizeof (powerpc_operands[0]));
+const unsigned int num_powerpc_operands = ARRAY_SIZE(powerpc_operands);
 
 /* The functions used to insert and extract complicated operands.  */
 
@@ -6969,7 +6968,7 @@ const struct powerpc_opcode powerpc_opco
 };
 
 const int powerpc_num_opcodes =
-  sizeof (powerpc_opcodes) / sizeof (powerpc_opcodes[0]);
+  ARRAY_SIZE(powerpc_opcodes);
 

 /* The VLE opcode table.
 
@@ -7208,7 +7207,7 @@ const struct powerpc_opcode vle_opcodes[
 };
 
 const int vle_num_opcodes =
-  sizeof (vle_opcodes) / sizeof (vle_opcodes[0]);
+  ARRAY_SIZE(vle_opcodes);
 

 /* The macro table.  This is only used by the assembler.  */
 
@@ -7277,4 +7276,4 @@ const struct powerpc_macro powerpc_macro
 };
 
 const int powerpc_num_macros =
-  sizeof (powerpc_macros) / sizeof (powerpc_macros[0]);
+  ARRAY_SIZE(powerpc_macros);

real	0m24.312s
user	19m2.785s
sys	0m23.889s

Use swap()

If you send a proper patch for this CC me: Gustavo A. R. Silva <[email protected]>

linux-next-20220104$ time make coccicheck -j 128 COCCI=scripts/coccinelle/misc/swap.cocci MODE=patch 2>&1 | tee swap.patch

some examples:

diff -u -p a/kernel/locking/test-ww_mutex.c b/kernel/locking/test-ww_mutex.c
--- a/kernel/locking/test-ww_mutex.c
+++ b/kernel/locking/test-ww_mutex.c
@@ -400,11 +400,8 @@ static int *get_random_order(int count)

        for (n = count - 1; n > 1; n--) {
                r = get_random_int() % (n + 1);
-               if (r != n) {
-                       tmp = order[n];
-                       order[n] = order[r];
-                       order[r] = tmp;
-               }
+               if (r != n)
+                       swap(order[n], order[r]);
        }

        return order;
diff -u -p a/drivers/net/ethernet/sfc/efx_channels.c b/drivers/net/ethernet/sfc/efx_channels.c
--- a/drivers/net/ethernet/sfc/efx_channels.c
+++ b/drivers/net/ethernet/sfc/efx_channels.c
@@ -819,11 +819,8 @@ int efx_realloc_channels(struct efx_nic
        old_txq_entries = efx->txq_entries;
        efx->rxq_entries = rxq_entries;
        efx->txq_entries = txq_entries;
-       for (i = 0; i < efx->n_channels; i++) {
-               channel = efx->channel[i];
-               efx->channel[i] = other_channel[i];
-               other_channel[i] = channel;
-       }
+       for (i = 0; i < efx->n_channels; i++)
+               swap(efx->channel[i], other_channel[i]);

        /* Restart buffer table allocation */
        efx->next_buffer_table = next_buffer_table;
@@ -865,11 +862,8 @@ rollback:
        /* Swap back */
        efx->rxq_entries = old_rxq_entries;
        efx->txq_entries = old_txq_entries;
-       for (i = 0; i < efx->n_channels; i++) {
-               channel = efx->channel[i];
-               efx->channel[i] = other_channel[i];
-               other_channel[i] = channel;
-       }
+       for (i = 0; i < efx->n_channels; i++)
+               swap(efx->channel[i], other_channel[i]);
        goto out;
 }

diff -u -p a/fs/ntfs3/lib/lzx_decompress.c b/fs/ntfs3/lib/lzx_decompress.c
--- a/fs/ntfs3/lib/lzx_decompress.c
+++ b/fs/ntfs3/lib/lzx_decompress.c
@@ -511,9 +511,7 @@ static int lzx_decompress_block(const st
                         * quirk allows all 3 recent offsets to be handled by
                         * the same code.  (For R0, the swap is a no-op.)
                         */
-                       match_offset = recent_offsets[offset_slot];
-                       recent_offsets[offset_slot] = recent_offsets[0];
-                       recent_offsets[0] = match_offset;
+                       swap(recent_offsets[offset_slot], recent_offsets[0]);
                } else {
                        /* Explicit offset  */

diff -u -p a/arch/x86/kernel/unwind_orc.c b/arch/x86/kernel/unwind_orc.c
--- a/arch/x86/kernel/unwind_orc.c
+++ b/arch/x86/kernel/unwind_orc.c
@@ -196,7 +196,6 @@ static struct orc_entry *cur_orc_table =
 static void orc_sort_swap(void *_a, void *_b, int size)
 {
        struct orc_entry *orc_a, *orc_b;
-       struct orc_entry orc_tmp;
        int *a = _a, *b = _b, tmp;
        int delta = _b - _a;

@@ -208,9 +207,7 @@ static void orc_sort_swap(void *_a, void
        /* Swap the corresponding .orc_unwind entries: */
        orc_a = cur_orc_table + (a - cur_orc_ip_table);
        orc_b = cur_orc_table + (b - cur_orc_ip_table);
-       orc_tmp = *orc_a;
-       *orc_a = *orc_b;
-       *orc_b = orc_tmp;
+       swap(*orc_a, *orc_b);
 }

 static int orc_sort_cmp(const void *_a, const void *_b)
diff -u -p a/tools/testing/radix-tree/main.c b/tools/testing/radix-tree/main.c
--- a/tools/testing/radix-tree/main.c
+++ b/tools/testing/radix-tree/main.c
@@ -163,11 +163,8 @@ void copy_tag_check(void)
 //     printf("generating radix tree indices...\n");
        start = rand();
        end = rand();
-       if (start > end && (rand() % 10)) {
-               cur = start;
-               start = end;
-               end = cur;
-       }
+       if (start > end && (rand() % 10))
+               swap(start, end);
        /* Specifically create items around the start and the end of the range
         * with high probability to check for off by one errors */
        cur = rand();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.