<div dir="ltr">hi all:<div> I finally implement it by learning form the function of tcp md5 checksum. This is the relative <a href="http://lxr.oss.org.cn/source/net/ipv4/tcp.c?v=2.6.30#L2667">kernel source</a>. The SHA-1 implement is <a href="https://github.com/lxlenovostar/kernel_test/blob/master/model/kernel_hash/sha.c">here</a>.But I still have a question about __virt_addr_valid. </div><div><br></div><div>##############################</div><div><div>#include <linux/init.h></div><div>#include <linux/module.h></div><div>#include <linux/kernel.h></div><div>#include <linux/crypto.h></div><div>#include <linux/err.h></div><div>#include <linux/scatterlist.h></div><div>#include <linux/mm.h></div><div><br></div><div>#define SHA1_LENGTH 20</div><div>static inline int phys_addr_valid(resource_size_t addr)</div><div>{</div><div>#ifdef CONFIG_PHYS_ADDR_T_64BIT</div><div> return !(addr >> boot_cpu_data.x86_phys_bits);</div><div> #else</div><div> return 1;</div><div> #endif</div><div>}</div><div><br></div><div>bool __virt_addr_valid(unsigned long x)</div><div>{</div><div> unsigned long old_x;</div><div> old_x = x;</div><div> //The kernel text is mapped into the region starting from__START_KERNEL_MAP</div><div> if (x >= __START_KERNEL_map) {</div><div> if (x >= MODULES_VADDR && x <= MODULES_END) {</div><div> printk(KERN_INFO "right here");</div><div> } </div><div> x -= __START_KERNEL_map;</div><div> if (x >= KERNEL_IMAGE_SIZE) {</div><div> printk(KERN_INFO "error1");</div><div> return false;</div><div> } </div><div> x += phys_base;</div><div> } else {</div><div> if (x < PAGE_OFFSET) {</div><div> printk(KERN_INFO "error2");</div><div> return false;</div><div> } </div><div> x -= PAGE_OFFSET;</div><div> if (!phys_addr_valid(x)) {</div><div> printk(KERN_INFO "error3");</div><div> return false;</div><div> } </div><div> } </div><div><br></div><div> return pfn_valid(x >> PAGE_SHIFT);</div></div><div>}<br></div><div><br></div><div><div>static int minit(void)</div><div>{</div><div> /*</div><div> * <a href="http://lxr.oss.org.cn/source/fs/ecryptfs/crypto.c?v=2.6.30">http://lxr.oss.org.cn/source/fs/ecryptfs/crypto.c?v=2.6.30</a></div><div> */</div><div> struct scatterlist sg;</div><div> struct hash_desc desc;</div><div><br></div><div> int rc = 0;</div><div> int i;</div><div> char hashtext[SHA1_LENGTH];</div><div><br></div><div><br></div><div> char *plaintext = NULL;</div><div> printk(KERN_INFO "valid=%s\n", virt_addr_valid(plaintext) ? "true" : "false");</div><div> plaintext = "c";</div><div> printk(KERN_INFO "valid=%s\n", virt_addr_valid(plaintext) ? "true" : "false");</div><div> size_t len = strlen(plaintext);</div><div><br></div><div> sg_init_one(&sg, (u8 *)plaintext, len);</div><div> desc.tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);</div><div> desc.flags = 0;</div><div><br></div><div> rc = crypto_hash_init(&desc);</div><div> if (rc) {</div><div> printk(KERN_ERR "%s: Error initializing crypto hash; rc = [%d]\n", __func__, rc);</div><div> goto out;</div><div> }</div><div> rc = crypto_hash_update(&desc, &sg, len);</div><div> if (rc) {</div><div> printk(KERN_ERR "%s: Error updating crypto hash; rc = [%d]\n", __func__, rc);</div><div> goto out;</div><div> }</div><div> rc = crypto_hash_final(&desc, hashtext);</div><div> if (rc) {</div><div> printk(KERN_ERR "%s: Error finalizing crypto hash; rc = [%d]\n", __func__, rc);</div><div> goto out;</div><div> }</div><div> crypto_free_hash(desc.tfm);</div><div><br></div><div> for (i = 0; i < 20; i++) {</div><div> //printk(KERN_INFO "%02x-%d\n", hashtext[i]&0xff, i);</div><div> }</div></div><div><br></div><div><div>out:</div><div> return rc;</div><div>}</div><div><br></div><div>static void mexit(void)</div><div>{</div><div> printk("Exit %s.\n", THIS_MODULE->name);</div><div>}</div></div><div>##############################</div><div><br></div><div>I make and insmod it. the log is:</div><div><br></div><div>##############################</div><div><div>[root@localhost sha]# dmesg -c</div><div>error2</div><div>valid=false</div><div>right here</div><div>error1</div><div>valid=false</div></div><div>##############################</div><div><br></div><div><img src="cid:ii_ig1yj3c60_1508ea12142ed8d5" width="562" height="188"></div><div><br></div><div>I think plaintext is between MODULES_VADDR and MODULES_END, So why the address in <br></div><div>MODULES not a valid virtual address?</div><div><br></div><div>Thank you.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-08-29 19:49 GMT+08:00 Bernd Petrovitsch <span dir="ltr"><<a href="mailto:bernd@petrovitsch.priv.at" target="_blank">bernd@petrovitsch.priv.at</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Fre, 2015-08-28 at 15:36 +0800, lx wrote:<br>
[...]<br>
<span class="">> //way 2<br>
> /*<br>
> char *plaintext = kmalloc(sizeof(char), GFP_KERNEL);<br>
> plaintext = "c";<br>
<br>
</span>I don't think that this line doesn't give any warning ...<br>
<br>
Kind regards,<br>
Bernd<br>
<span class="HOEnZb"><font color="#888888">--<br>
"I dislike type abstraction if it has no real reason. And saving<br>
on typing is not a good reason - if your typing speed is the main<br>
issue when you're coding, you're doing something seriously wrong."<br>
- Linus Torvalds<br>
<br>
</font></span></blockquote></div><br></div>